Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
Last active January 24, 2016 05:32
Show Gist options
  • Save DmitrySoshnikov/79ee5e9779062757de4b to your computer and use it in GitHub Desktop.
Save DmitrySoshnikov/79ee5e9779062757de4b to your computer and use it in GitHub Desktop.
ES2015: Destructuring reference bindings
/**
* Destructuring reference bindings.
*
* by Dmitry Soshnikov <[email protected]>
* MIT Style License
*/
// Bindings of a pattern in destructuring may be not only
// simple variables, but any reference types. In the following
// example we use destructuring assignment to swap two elements
// of an array: the pattern bindings are not variable names in
// in case, but references to array elements.
let a = [1, 2, 3];
[a[0], a[a.length - 1]] = [a[a.length - 1], a[0]];
console.log(a); // [3, 2, 1]
// This is similar to initializer part of a `for-in` or `for-of`,
// where we also could use not only variables but other
// references. As an example, unusual properties collector:
let props = [], i = 0;
for (props[i++] in {x: 1, y: 2});
console.log(props); // ['x', 'y']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment