admin管理员组文章数量:1289827
I am running a self executing function in javascript to not interfere with any global variables on a page. I have a string with the name of a function I want to call. The function is declared inside my self-executing function. Is there a way to call that function by using the string?
(function(document, window){
var functionName = "myFunction";
window[functionName](); //This does not work...
function myFunction(){
//Do some stuff
}
}(document, window)
I found this: How to execute a JavaScript function when I have its name as a string
but my function is self executing without a name, so I have no way to reference it with the window variable.
I am running a self executing function in javascript to not interfere with any global variables on a page. I have a string with the name of a function I want to call. The function is declared inside my self-executing function. Is there a way to call that function by using the string?
(function(document, window){
var functionName = "myFunction";
window[functionName](); //This does not work...
function myFunction(){
//Do some stuff
}
}(document, window)
I found this: How to execute a JavaScript function when I have its name as a string
but my function is self executing without a name, so I have no way to reference it with the window variable.
Share Improve this question edited May 23, 2017 at 12:18 CommunityBot 11 silver badge asked Nov 5, 2010 at 19:23 ClintClint 331 silver badge3 bronze badges 2-
Can't you just do
var f = myFunction
(after defining it)? – user395760 Commented Nov 5, 2010 at 19:27 -
Are you trying to call the self-executing function or
myFunction
? – xj9 Commented Nov 5, 2010 at 19:50
8 Answers
Reset to default 4This is just a cleaner version of what others have suggested, no global pollution, no eval, and no obscure variable references (arguments.callee and "this").
// Not sure why you were passing in doc and window, so I took it out for clarity
(function(){
var funcs = {
funcA: function () { return "a"},
funcB: function () { return "b"}
};
var functionName = "funcA";
funcs[functionName]();
}();
This approach does require you to declare the functions before you use them.
Without global pollution and avoids the non-strict arguments.callee
method.
Based on this answer.
// This Immediately Invoked Function Expression is invoked via `call`.
// call() takes 1 or more arguments: the first being the scope you want the
// invoked function be in; the other arguments bee arguments of the function
// itself.
// In this case, the scope of our function is an empty object ({}).
// This object allows us to attach functions using the this.* syntax and call the
// functions with this["string"]() without ever polluting the global namespace.
// It is also forward patible with ECMAScript 5's scrict-mode as it does not
// use arguments.callee.
(function(document, window){
var functionName = "myFunction";
// you'll need to define the function _before_ you call it.
this.myFunction = function(){
//Do some stuff
alert('me');
};
this[functionName]();
}.call({}, document, window));
If you don't mind defining the function before you call it, you can use arguments.callee
. This avoids eval.
(function (){
arguments.callee.f = function() {
console.log('test');
};
arguments.callee['f']();
})();
Can you just store the myFunction
function somewhere (such as on a new object)?this
See jsFiddle example:
(function (document, window){
var functionName = "myFunction",
myFunctions = {
myFunction: function() { alert('myFunction'); }
};
myFunctions[functionName]();
}(document, window));
You can use eval(functionName + "()")
since any function of window
is also a global function.
I guess I can't ment on answers, so I have to post it this way. Eval is too dangerous for me, since the actual function name is given by the user in a string.
You are correct. This is because your function is defined inside your self-executing function. Window has no knowledge of the inner closure. The best you could do is probably eval:
a la:
eval(functionName + "();");
, and it's probably not all that evil in this case: When is JavaScript's eval() not evil?
Try:
functionName.apply(context
, arguments
)
or
functionName.call(context
[, argument 1
, argument 2
, ...]) (view reference)
Or (as a poster below mentioned) you can add the function to an object in the window namespace:
var newObj = {}; newObj.myFunction = function() { // stuff }
newObj.myFunction(); //calls the new function
本文标签: Javascript function name as a string in self executing functionStack Overflow
版权声明:本文标题:Javascript function name as a string in self executing function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741418295a2377651.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论