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(); } };
source share