通过名称调用不属于全局范围的函数 [英] Calling a function by name that is not part of the global scope
本文介绍了通过名称调用不属于全局范围的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有这样的东西:
var MyApp = MyApp || {};
MyApp.feature = (function($){
var somelocalVariable,
otherone
init = function() {
},
somePrivateFunction = function() {
var functionName = 'otherFunction';
//how can I call otherFunction by name (using variable above)
},
otherFunction = function() {
};
return {
init: init
}
})(jQuery);
在此示例中,如果我执行window['otherFunction']
,则不会发生任何事情,因为它不是全局函数.如何将其作为更私有范围的一部分执行?
In this example if I do window['otherFunction']
nothing will happen because its not a global function. How can I execute it as a part of the more private scope?
推荐答案
用"this"替换"window"
Replace "window" with "this"
this[functionName]();
这篇关于通过名称调用不属于全局范围的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文