=> arrow Functions
- syntaxis más corta con
=> - siempre anónimas
- Lexically bind
this - Muy útil para event handlers y callbacks
- Muy útil para programación funcional (higher order functions(
4 versiones:
(arg1, arg2, ...) => expr
(arg1, arg2, ...) => { stmt1; stmt2; ... }
singleArg => expr
singleArg => { stmt1; stmt2; ... }
value of this
antes (ES5)
var self = this;
this.element.addEventListener('click', function(event) {
self.registerClick(event.target.id);
});
después (ES2015)
this.element.addEventListener('click', event => {
this.registerClick(event.target.id);
});
higher order functions
antes (ES5)
[1,3,4,5,6,7].filter(function(n) { return n % 2 } )
.map(function(n, i) { return n + i } );
// [1, 4, 7, 10]
después (ES2015)
[1,2,3,4,5,6,7].filter(n => n % 2).map((n, i) => n+i);
Más info:
- ECMAScript 6 equivalents in ES5: Arrow Functions
- ES6 In Depth: Arrow functions
- Understanding ECMAScript 6 arrow functions
⛏ ES6 Katas: Arrow Functions
Hacer las siguientes katas: