How to make a variable that is inside a global function?

Below is my javascript function. I want to use the selected external function variable, but I get the selected undefined error in the check item console. window.yourGlobalVariable does not solve my problem.

function showMe(pause_btn) { var selected = []; for (var i = 0; i < chboxs.length; i++) { if (chboxs[i].checked) { selected.push(chboxs[i].value); } } } 
+6
source share
6 answers

If you really want it to be global, you have two options:

  • Declare it globally, and then leave var in the function:

     var selected; function showMe(pause_btn) { selected = []; for (var i = 0; i < chboxs.length; i++) { if (chboxs[i].checked) { selected.push(chboxs[i].value); } } } 
  • Assign window property

     function showMe(pause_btn) { window.selected = []; for (var i = 0; i < chboxs.length; i++) { if (chboxs[i].checked) { selected.push(chboxs[i].value); // Don't need `window.` here, could use it for clarity though } } } 

    The window properties are global variables (you can access them with or without window. front of them). A.

But , I would not become global. Or showMe return information:

 function showMe(pause_btn) { var selected = []; for (var i = 0; i < chboxs.length; i++) { if (chboxs[i].checked) { selected.push(chboxs[i].value); } } return selected; } 

... and then where you need it:

 var selected = showMe(); 

... or declare it in an area containing showMe , but not globally. Without context, it looks like # 1 above; here is a little context:

 (function() { var selected; function showMe(pause_btn) { selected = []; for (var i = 0; i < chboxs.length; i++) { if (chboxs[i].checked) { selected.push(chboxs[i].value); } } return selected; } // ...other stuff that needs `selected` goes here... })(); 

An external anonymous function is a “scope function”, which means that selected not global, it is just common to any function.

+13
source

Do it:

 var selected; function showMe(pause_btn) { selected = []; for (var i = 0; i < chboxs.length; i++) { if (chboxs[i].checked) { selected.push(chboxs[i].value); } } } 

In fact, you can skip the var selected; line var selected; but I prefer to declare my variables.

+1
source

Do not use this;

  selected = []; 

this is javascript error

 window.selected = []; 

inside your function.

+1
source

You can define an array named selected in the scope defined by the showMe function.

In terms of code:

 var selected = []; function showMe(pause_btn) { for (var i = 0; i < chboxs.length; i++) { if (chboxs[i].checked) { selected.push(chboxs[i].value); } } } 
0
source
 var selected = []; function showMe(pause_btn) { for (var i = 0; i < chboxs.length; i++) { if (chboxs[i].checked) { selected.push(chboxs[i].value); } } } 
0
source

If you declare selected as a property of a window object, you can access it from another location.

 function showMe(pause_btn) { window.selected = []; for (var i = 0; i < chboxs.length; i++) { if (chboxs[i].checked) { selected.push(chboxs[i].value); } } } 
0
source

Source: https://habr.com/ru/post/985404/


All Articles