Skip to content

Instantly share code, notes, and snippets.

@gnab
Last active December 30, 2015 21:48
Show Gist options
  • Save gnab/7889545 to your computer and use it in GitHub Desktop.
Save gnab/7889545 to your computer and use it in GitHub Desktop.
Destructuring.
var [first, second, ...rest] = [1, 2, 3, 4, 5];
console.log('first =', first);
console.log('second =', second);
console.log('rest =', rest);
// => first = 1
// => second = 2
// => rest = [3, 4, 5]
function printPoint({x: x, y: y}) {
console.log('x = %d, y = %d', x, y);
}
printPoint({x: 2, y: 3});
// => x = 2, y = 3
function addPoints ([x1, y1], [x2, y2]) {
return [x1 + x2, y1 + y2];
}
console.log('addPoints([1, 2], [3, 4]) =', addPoints([1, 2], [3, 4]));
// => addPoints([1, 2], [3, 4]) = [4, 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment