4

I am attempting to declare a function outside of anonymous function but still have acess to all of the anonymous functions variables

Below is demonstrating what I'm talking about.

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 scope clean from tons of variables, but still have the convenience of accessing, set and calling as if they were global. I have concluded there is a way to doing what I'm after but it requires a deprecated functionality in javascript. Here is some example code showing how to accomplish the above without eval. This article discusses how to use "with".

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();​
Community
  • 1
  • 1
Lime
  • 12,724
  • 9
  • 48
  • 84
  • You have a local variable with the name "appearingToBeGlobalFunction"... that is a paradox. Why do you think that that the function that is assigned to that local variable should be global? It is local. – Šime Vidas Sep 11 '10 at 16:39
  • @Šime Vidas: He did not use `var` to define that variable, therefore it's defined in the global context. It should be renamed to `appearingToBeGlobalFunctionAndActuallyIs`. – Cristian Sanchez Sep 11 '10 at 16:49
  • Yes he did. Both "variableScope" and "appearing..." are local variables. (Notice the comma seperator between them). http://stackoverflow.com/questions/694102/declaring-multiple-variables-in-javascript – Šime Vidas Sep 11 '10 at 16:58
  • @Šime Vidas: You're right, sorry. That's why indentation is important people! I think the reason he added that `appearingToBeGlobalFunction` function with that name is because when called from the "outside" it *appears* to be a global function if you didn't know what he was trying to do. – Cristian Sanchez Sep 11 '10 at 17:03
  • You have no idea good your question is. The entire internet is wrong. Binding this is not changing the scope of a function its setting a pointer(this) to an object to include into a functions scope. That scope is not changed or re bound at all period. – Heavy Gray Mar 08 '12 at 13:05

4 Answers4

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

Oh dear Lord.

The reason this “works” is that you are converting the function fun (alertTest) into a string to put it in the eval argument.

It happens that in most desktop browsers, a native JS function's toString() result will be a string that looks like a function expression containing the same code as the original declaration. You're turning a function back into a string and re-parsing that string in the context of the new enclosing function, so the new function value is the same code but with a different closure.

However, it is not required that Function#toString work like this, and in some cases it won't. It is not safe to rely on function decomposition; avoid.

You can certainly only do this kind of horrific hackery using eval, although there is no reason the window[funName]= part has to be inside the eval. window[funName]= eval('('+fun+')'); would work equally well (badly).

I am attempting to declare a function outside of anonymous function but still have acess to all of the anonymous functions variables

Whyever would you do something crazy like that?

bobince
  • 498,320
  • 101
  • 621
  • 807
2

You can't get rid of eval and still expect it to work. That's the only way to take a look at members of the scope after it's been "closed." I've messed around with something similar in the past, but I would never actually use it anywhere. Consider an alternate solution to whatever you're trying to accomplish.

Community
  • 1
  • 1
Cristian Sanchez
  • 27,279
  • 10
  • 53
  • 59
0

you could force the variables to be in the global scope eg instead of var variableScope = 'insideFunction' you use window.variableScope = 'insideFunction'

Alex
  • 1,317
  • 2
  • 11
  • 20
0

The goal of this question was to ultimately keep the global scope clean from tons of variables, but still have the convenience of accessing, set and calling as if they were global. I have concluded there is a way to doing what I'm after but it requires a deprecated functionality in javascript. Here is some example code showing how to accomplish the above without eval. This article discusses how to use "with".

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();​
Community
  • 1
  • 1
Lime
  • 12,724
  • 9
  • 48
  • 84