Using ES6 arrow functions with lexical this
binding is great.
However, I ran into an issue a moment ago using it with a typical jQuery click binding:
class Game {
foo() {
self = this;
this._pads.on('click', function() {
if (self.go) { $(this).addClass('active'); }
});
}
}
Using an arrow function instead:
class Game {
foo() {
this._pads.on('click', () => {
if (this.go) { $(this).addClass('active'); }
});
}
}
And then $(this)
gets converted to ES5 (self = this) type closure.
Is a way to have Traceur ignore "$(this)" for lexical binding?
.on()
does indeed have athis
value useful to you. To me it's much clearer withthis
referring to the event target than having to pass the event and find the target manually. I haven't played much with arrow functions but it does seem like it would be confusing to go back and forth with anonymous functions. – robisrob