使用带有词法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)"进行词法绑定的方法吗?