-
Notifications
You must be signed in to change notification settings - Fork 1
Discussion
##22.5 Don't save references to this. Use arrow functions or Function#bind.
- Abby
I vote for just 'use arrow functions.' Once the nesting and chaining start, bind gets pretty junky looking. and according to the article http://www.2ality.com/2012/04/arrow-functions.html (good read), "The arrow function is mostly syntactic sugar for bind... but consumes less memory"
a.save().then(function() {
b.save().then(function() {
}.bind(this)).catch(function() {
}.bind(this)).finally(function() {
}.bind(this));
}.bind(this)).catch(function() {
}.bind(this)).finally(function() {
}.bind(this));
versus
a.save().then(() => {
b.save().then(() => {
}).catch(() => {
}).finally(() => {
});
}).catch(() => {
}.finally(() => {
});
Ember specific: However, (as far as I understand) arrow functions probably aren't the way to go in Ember at this time, for they cannot be used with 'property' and 'observes' declarations (ref https://github.com/babel/ember-cli-babel/issues/30). Such declarations are readily used in ember, which would lead to fairly inconsistent application of the syntax, which wouldn't look great. Ember is heading toward an alternative syntax for those declarations with ember-computed-decorators.
But in general,
- Use non-arrow functions for methods that will be called using the object.method() syntax. These functions will then receive a meaningful 'this' value from their caller.
- Use arrow functions for everything else.