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 ...
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:
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.