Backbone Arrow Function And This
Solution 1:
I'm puzzled why you want to use arrow functions here at all. From the fine manual:
An arrow function expression has a shorter syntax than a function expression and does not bind its own
this
,arguments
,super
, ornew.target
. Arrow functions are always anonymous. These function expressions are best suited for non-method functions, and they cannot be used as constructors.
and:
Arrow functions used as methods
As stated previously, arrow function expressions are best suited for non-method functions. [...]
Arrow functions do not define ("bind") their own
this
.
The whole point of arrow functions is to have a short-form function without all the OO stuff (like this
), they're really meant for cases where you don't care about this
such as:
some_array.map(e => e * 2)
Given this:
// (a)
var filterBar = Backbone.View.extend({
initialize: () => {
// (b)
}
});
the value of this
at (b) is exactly the same as at (a). this
inside one of your view methods will never be the view instance without significant kludgery and gymnastics (and even then it might not even be possible to make initialize
an arrow function).
Summary: Don't do that, that's not what arrow functions are for.
Post a Comment for "Backbone Arrow Function And This"