按名称调用函数,该函数不属于全局范围

时间:2014-07-25 02:59:27

标签: javascript jquery

让我说我有这样的事情:

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']什么都不会发生,因为它不是一个全局函数。如何将其作为更私密范围的一部分执行?

1 个答案:

答案 0 :(得分:1)

将“window”替换为“this”

this[functionName]();
相关问题