从 setTimeout() 内部调用 this._super()

jre*_*ing 4 javascript inheritance settimeout

我们使用 John Resig 的 inherit.js。这使我们可以访问方便的_super()函数来调用父函数。太棒了,但今天我被一个问题难住了,我this._super()无法从 a 内部调用setTimeout,即使我绑定了这个:

代码示例

var Person = Class.extend({
  init: function(isDancing){
  this.dancing = isDancing;
},
    dance: function(){
        return this.dancing;
    }
});

var Ninja = Person.extend({
    init: function(){
        this._super( false );
},
dance: function(){
    window.setTimeout(function(){
        // Call the inherited version of dance()
        return this._super();
    }.bind(this),50);
});
Run Code Online (Sandbox Code Playgroud)

this._super()未定义!到底是怎么回事?

oli*_*ren 5

要完成这项工作,您需要在子类方法中捕获 _super 方法,如下所示:

dance: function(){
    // capture the super method for later usage
    var superMethod = this._super;
    window.setTimeout(function(){
        return superMethod();
    },50);
};
Run Code Online (Sandbox Code Playgroud)

这有效而您的代码无效的原因是,extend()inherit.js中的方法在this._super运行覆盖的方法之前捕获了超类的方法。然后它运行您的代码,并在您的代码运行后将 _super 恢复为它在运行之前设置的任何值。该操作发生在以下来自 inherit.js 的代码中

      var tmp = this._super;

      // Add a new ._super() method that is the same method
      // but on the super-class
      this._super = _super[name];

      // The method only need to be bound temporarily, so we
      // remove it when we're done executing
      var ret = fn.apply(this, arguments);        
      this._super = tmp;

      return ret;
Run Code Online (Sandbox Code Playgroud)

所以更具体地说;运行原始代码时,用作 setTimeout 参数的函数绑定到原始对象。它不起作用的原因是,即使this引用了正确的对象,也this._super引用了其他东西,因为this._super在方法运行之前被重置为指向它所指向的任何东西。可能它没有设置,所以 的值this._super很可能只是undefined