Table of Contents generated with DocToc
Object literals are extended to support setting the prototype at construction, shorthand for foo: foo assignments, defining methods, making super calls, and computing property names with expressions.[Source]
Example:
var obj = {
// Shorthand for ‘handler: handler’
handler,
// Methods
toString() {
},
// Computed (dynamic) property names
[ 'prop_' + (() => 42)() ]: 42
};
Destructuring allows binding using pattern matching, with support for matching arrays and objects. [Source]
var [a, b, c] = [1,2,3];
//swap variables
var [a, b] = [b, a];
// object matching
var { someKey: variable1 } = {
someKey: 17
};
// can be used in parameters (along with enhanced object literals)
function printPrettyMessage(str, { decorator }) {
console.log(decorator + str + decorator);
}
// Fail-soft destructuring
var [a] = [];
a === undefined;
// Fail-soft destructuring with defaults
var [a = 1] = [];
a === 1;
It allows to declare default values when declaring variables.
// can be used in parameters position
function f(x, y=12) {
// y is 12 if not passed (or passed as undefined)
return x + y;
}
f(3) === 15; // true
// or in assignments
var { someKey: variableName = 'this is the default value'} = {
someKey: undefined
};
console.log(variableName); // 'this is the default value'
Turns an array into consecutive arguments in a function call.
function f(a,b,c){
console.log(a,b,c);
}
f(...[1,2,3]);
It transforms arguments into an array. Replaces the need for the arguments
object.
function f(...x) {
console.log(x);
}
f(1,2,3);
Arrow functions are a shorthand for function expressions and they support both statement block bodies as well as expression bodies which return the value of the expression. Unlike regular functions, arrows share the same lexical this as their surrounding code. They cannot be named.
// Expression body
var odds = evens.map(v => v + 1);
// Statement bodies
nums.forEach(v => {
if (v % 5 === 0)
fives.push(v);
});
// Lexical this
var bob = {
_name: "Bob",
_friends: [],
printFriends() {
this._friends.forEach(f =>
console.log(this._name + " knows " + f));
}
}