ES6 Destructuring

node v0.12.18
version: master
endpointsharetweet
Destructuring allows binding using pattern matching, with support for matching both arrays and objects.
// We don't care about the second item, so just leave it out var [first, , third] = [1, 2, 3]; console.log(first + " and " + third);
var person = { name: "Tom", age: 30, city: "San Francisco" }; var { name, city } = person; console.log(name + " lives in " + city);
Destructuring can be used in function parameters to make it easier to just get what you want.
function isOldEnoughToVote({ age }) { return age >= 18; } if (isOldEnoughToVote(person)) console.log(person.name + " is old enough to vote"); else console.log(person.name + " is not old enough to vote");
Learn more about destructuring here: https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/ Some of these examples and explanations originally taken and modified from: https://github.com/lukehoban/es6features#destructuring
Loading…

no comments

    sign in to comment