Skip to content

Instantly share code, notes, and snippets.

@tyage
Last active August 29, 2015 14:05
Embed
What would you like to do?
// executed by traceur 0.0.59
// basic
[this.a, b] = [1, 2]
console.log(this.a, b) // => 1 2
var {a, b: [c], d: d} = {a: 1, b: [2], d: 3}
console.log(a, c, d) // => 1 2 3
var [a, , {b: c}, d] = [1, 3, {b: 2}]
console.log(a, c, d) // => 1 2 undefined
// parameter
var a = ({a}) => console.log(a)
a({a: 1, b: 2}) // => 1
// string
var [a, b, , , e] = "abcdef"
console.log(a, b, e) // => a b e
var {length} = "some string"
console.log(length) // => 11
// default
var {a = 1} = {a: 2, b: 1}
console.log(a) // => 2
var {a = 1} = {b: 1}
console.log(a) // => 1
var [a = 1] = []
console.log(a) // => 1
// rest
var [first, second, ...rest] = [1,2,3,4,5]
console.log(first, second, rest) // => 1 2 [ 3, 4, 5 ]
var [first, second, , ...rest] = [1,2,3,4,5]
console.log(first, second, rest) // => 1 2 [ 4, 5 ]
// with computed properties
var {[0 + 1]: a} = [1, 2, 3]
console.log(a) // => 2
// error
try {
var [[a]] = []
} catch({message}) {
console.log(message) // Cannot read property '0' of undefined
}
try {
var [[a]] = [null]
} catch({message}) {
console.log(message) // Cannot read property '0' of null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment