小编JRo*_*l3r的帖子

使用jQuery $(this)和ES6 Arrow函数(词汇这个绑定)

使用带有词法this绑定的ES6箭头功能非常棒.

但是,我刚刚遇到一个问题,使用它与典型的jQuery点击绑定:

class Game {
  foo() {
    self = this;
    this._pads.on('click', function() {
      if (self.go) { $(this).addClass('active'); }
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

改为使用箭头功能:

class Game {
  foo() {
    this._pads.on('click', () => {
      if (this.go) { $(this).addClass('active'); }
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

然后$(this)转换为ES5(self = this)类型闭包.

是一种让Traceur忽略"$(this)"进行词法绑定的方法吗?

javascript jquery ecmascript-6 arrow-functions

103
推荐指数
5
解决办法
4万
查看次数