让我说我有这样的事情:
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']
什么都不会发生,因为它不是一个全局函数。如何将其作为更私密范围的一部分执行?
答案 0 :(得分:1)
将“window”替换为“this”
this[functionName]();