ES6 - Destructuring
Set variables values by object destructuring.
Destructuring first level
const pony = { foo: 'foo test' }
const { foo } = pony
//foo value is equal to = pony.foo = 'foo test'
Destructuring with alias
const pony = { foo: 'foo test with alias' }
const { foo: baz } = pony
//baz value is equal to pony.foo = 'foo test with alias'
Destructuring with default value
const pony = {}
const { foo='default value' } = pony
//foo value is equal to pony.foo with 'default value' default value
Destructuring Level 2:
const pony = {
foo: {
baz: 'baz'
}
}
const { foo: baz } = pony
//baz value will be 'baz'
Destructuring response level 0 and nested
1. Destructuring level 0 raise undefined is the variable not does not exist
2. Destructing nested level raise an error when the variable does not exist
Destructing in function parameters
function foo(bar={a: 1, b: 2}) {
}