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 acplish 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();
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 acplish 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();
Share
Improve this question
edited May 23, 2017 at 12:10
CommunityBot
11 silver badge
asked Sep 11, 2010 at 14:59
LimeLime
13.6k12 gold badges58 silver badges92 bronze badges
5
- 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 Commented Sep 11, 2010 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 toappearingToBeGlobalFunctionAndActuallyIs
. – Cristian Sanchez Commented Sep 11, 2010 at 16:49 - Yes he did. Both "variableScope" and "appearing..." are local variables. (Notice the ma seperator between them). stackoverflow./questions/694102/… – Šime Vidas Commented Sep 11, 2010 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 Commented Sep 11, 2010 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. – James Andino Commented Mar 8, 2012 at 13:05
4 Answers
Reset to default 2You 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 acplish.
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 deposition; 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?
you could force the variables to be in the global scope eg instead of var variableScope = 'insideFunction'
you use window.variableScope = 'insideFunction'
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 acplish 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();