A few of my favorite ES6 features that help me write less boilerplate code.
Property shortcuts
1 | const name = 'joe' |
1 | // es6 |
Read http://es6-features.org/#PropertyShorthand
Destructuring
1 | // es5 |
1 | // es6 |
Read http://es6-features.org/#ObjectMatchingShorthandNotation and Parameter destructuring.
Default values
1 | // es5 |
1 | // es6 |
Read http://es6-features.org/#DefaultParameterValues
Separate arguments
1 | // ES5: need all arguments after "y" |
We could use a utility library
1
2
3
4
5
6const _ = require('lodash')
function f(x, y) {
// need all arguments after "y"
var a = _.toArray(arguments).slice(2);
...
};1
2
3
4
5// ES6
function f(x, y, ...a) {
// a is an array of arguments after "y"
...
};
Computed keys
1 | // es5 |
1 | // es6 |
Read http://es6-features.org/#ComputedPropertyNames
Arrow functions
1 | numbers.map(function (v) { return v + 1 }) |
For clarity I prefer Ramda functions
1
2
3var R = require('ramda')
var add1 = R.add(1)
numbers.map(add1)
1 | const foo = options => value => { |