Global closing object

There is something very basic about JS that I'm not sure about. Therefore, I would like to ask.

The global window object (or global , of course, for node.js). Thus, we can, for example, get the value of a variable named window[myVariableName] .

The question is, how can I do this with a local closure variable?

 function myFunction() { // some code here console.log(global_object[myVariableName]); } 

And it may even be possible to get the value of a variable by name when the variable is global for the current close, but does not belong to the window object.

From what I know about JS and how it works, this is not possible.

By the way, a very popular question is how to get the value of a variable by name, and never mentioned that the variable is global :).
+4
source share
3 answers
 eval('var value = ' + myVariableName + ';'); console.log(value); 

Yes, eval is evil, but it is a solution (for a strange task).

+3
source

You can do it with all evil eval

 function test() { var a = 5; var myname = "a"; console.log(eval(myname)) } test() 
+2
source

A variable cannot be "global" for closure.
It is either global or not.

If it is not, then it is within the scope of the function.

If it is within the scope of the function, then the only way to get it, as soon as you are outside of the function, is to make this property that you return from the function, or add it to the object / array that you passed to the function, or create the INSIDE function of the function that returns this variable ...

 // modifying an object var my_func = function (obj) { var hidden = 1; obj.hidden = hidden; }; var my_obj = {}; my_func(my_obj); my_obj.hidden; // 1 // returning a function var my_func = function () { var a = 1, b = 2, c = 3, get_a = function () { return a; }, get_b = function () { return b; }, get_c = function () { return c; }; return { a : get_a, b : get_b, c : get_c }; }; var my_obj = my_func(); my_obj.a(); // 1 my_obj.b(); // 2 my_obj.c(); // 3 

If you prefer to do this using the "name", you must create one object in your closure, and you would create a function that would take a string and look for the properties of this object by name.

 // get property by name var my_func = function () { var properties = { a : 1, b : 2, c : 3 }; return { get : function (name) { return properties[name]; // returns undefined if it doesn't exist } }; }; var my_obj = my_func(); my_obj.get("a"); // 1 my_obj.get("b"); // 2 

Now you can.

PS: eval(); not always guaranteed to work as described above.
For example, in the future, eval should be launched in its own area and not have access to the area of โ€‹โ€‹the calling function (build new Function("..."); also works).

EDIT

To take this step further, it is better to answer the question about "global" , as it relates to scope:

 window.bob = "Bob"; var outer_A = function () { var mid_A = function () { var inner_A = function () { console.log("INNER-A"); console.log("bob = " + bob ); console.log("doug = " + doug); }; inner_A(); }; mid_A(); }, outer_B = function () { var mid_B = function () { var doug = "Doug", inner_B = function () { console.log("INNER-B"); console.log("bob = " + bob ); console.log("doug = " + doug); }; inner_B(); }, mid_C = function () { var inner_C = function () { console.log("INNER-C"); console.log("bob = " + bob ); console.log("doug = " + doug); }; inner_C(); }; mid_B(); mid_C(); }; outer_A(); outer_B(); 

What does it mean?
You should get a printout that looks like this:

 /* INNER-A bob = Bob doug = undefined INNER-B bob = Bob doug = Doug INNER-C bob = Bob doug = undefined */ 

Why do you need this?
Well, pretty simple, because you have branch function fields.
doug defined inside mid_B .
Any functions created inside mid_B can potentially access doug .
No functions created outside of mid_B can access doug .

When inner_C requested by log doug , it first checks to see if doug exists in its own function.
If not, it checks to see if it exists in its parent sphere function ( mid_C ).
If not, it checks to see if it exists in its parent region function ( outer_B ).
If not, it checks to see if it exists in its parent region function ( window ).
If you go back to window , it has no parent region function ...
... so if it's not even in window , set the value to undefined .

Meanwhile, inner_B cannot find doug , so it checks mid_B and finds doug .

This does not make doug "global."
This makes it in-scope .

"global" would be if outer_A , mid_A , inner_A , outer_B , mid_B , inner_B , mid_C and inner_C all had access ...

... it will be a bob .
bob is global.

It is global because it is bound to a window object (or defined as var in the global-scope , which works in much the same way as when using delete ).
And since all child functions have functional areas returning to window , all functions can access something defined as the / var window property (unless the variable extends the chain of areas with the same name, point, it selects the first one that gets )

This is what global means, and why doug not a global variable in this example.

0
source

All Articles