レキシカルthis
バインディングで ES6 アロー関数を使用するのは素晴らしいことです。
ただし、典型的な jQuery クリック バインディングで使用する少し前に問題が発生しました。
class Game {
foo() {
self = this;
this._pads.on('click', function() {
if (self.go) { $(this).addClass('active'); }
});
}
}
代わりに矢印関数を使用します。
class Game {
foo() {
this._pads.on('click', () => {
if (this.go) { $(this).addClass('active'); }
});
}
}
そして、$(this)
ES5 (self = this) 型のクロージャーに変換されます。
字句バインディングのために Traceur に "$(this)" を無視させる方法はありますか?