Javascript function variable scope

I'm trying to declare a function outside of an anonymous function, but still mean all the anonymous function variables

Below is what I say.

I just need to get rid of eval.

//Used to determine where the variable is being stored var variableScope = "global"; (function(window){ var variableScope = 'insideFunction', appearingToBeGlobalFunction = function(){ alert("This Function appears Global but really isn't"); }; window["addFunction"]=function(funName,fun){ //window[funName] = fun; Doesn't work eval("window[funName]="+fun+";"); } })(window); addFunction("alertTest",function(){ alert(variableScope); appearingToBeGlobalFunction(); }); //should alert "insideFunction" and "This Function appears Global but really isn't" alertTest(); 

Edit: The goal of this question was to ultimately keep the global area clean of many variables, but still have the convenience of access, installation, and invocation, as if they were global. I came to the conclusion that there is a way to do what I need, but this requires legacy functionality in javascript. Here is a sample code showing how to execute the above without eval. This article discusses how to use c.

 var variableScope = "global"; var customScope = { variableScope : 'insideFunction', appearingToBeGlobalFunction : function(){ alert("This Function appears Global but really isn't"); } }; function alertTest(){ with(customScope){ alert(variableScope); appearingToBeGlobalFunction(); } }; //should alert "insideFunction" and "This Function appears Global but really isn't" alertTest();​ 
+4
source share
4 answers

You cannot get rid of eval and still expect it to work. This is the only way to look at the members of the sphere after it has been "closed." In the past, I came across something > similar , but I would never use it anywhere. Consider an alternative solution to what you are trying to accomplish.

+2
source
 eval("window[funName]="+fun+";"); 

Oh my God.

The reason this "works" is because you convert the fun ( alertTest ) function to a string to put it in the eval argument.

It happens that in most desktop browsers, the initial result of the toString() JS function will be a string that looks like a function expression containing the same code as the original declaration. You return the function back to the string and re-parse this string in the context of the new closing function, so the new value of the function is the same code, but with a different closure.

However, Function#toString not required to work this way, and in some cases it will not . It is unsafe to rely on decomposition of functions; to avoid.

You can, of course, make this terrible hacker using eval , although there is no reason for the window[funName]= part to be inside eval . window[funName]= eval('('+fun+')'); will work equally well (bad).

I'm trying to declare a function outside of an anonymous function, but still mean all the anonymous function variables

Why don't you do something crazy?

+2
source

you can force the variables to be in the global scope, for example, instead of var variableScope = 'insideFunction' you use window.variableScope = 'insideFunction'

0
source

The purpose of this question was to ultimately keep the global scope clear of many variables, but still have the convenience of access, installation, and invocation, as if they were global. I came to the conclusion that there is a way to do what I need, but this requires legacy functionality in javascript. Here is a sample code showing how to execute the above without eval. This article discusses how to use c.

 var variableScope = "global"; var customScope = { variableScope : 'insideFunction', appearingToBeGlobalFunction : function(){ alert("This Function appears Global but really isn't"); } }; function alertTest(){ with(customScope){ alert(variableScope); appearingToBeGlobalFunction(); } }; //should alert "insideFunction" and "This Function appears Global but really isn't" alertTest();​ 
0
source

All Articles