Merge pull request #120 from AhsanAyaz/es6-destructuring-modif
added desctructures with defaults for array & object
This commit is contained in:
commit
206339926c
31
es6.md
31
es6.md
|
@ -202,6 +202,20 @@ let {title, author} = {
|
|||
Supports for matching arrays and objects.
|
||||
See: [Destructuring](http://babeljs.io/learn-es2015/#ecmascript-2015-features-destructuring)
|
||||
|
||||
### Default values
|
||||
|
||||
```js
|
||||
var scores = [22, 33]
|
||||
var [math = 50, sci = 50, arts = 50] = scores
|
||||
```
|
||||
|
||||
#### Result
|
||||
|
||||
```
|
||||
math === 22, sci === 23, arts === 50
|
||||
```
|
||||
|
||||
Default values can be assigned while destructuring arrays or objects.
|
||||
|
||||
### Function arguments
|
||||
|
||||
|
@ -216,6 +230,23 @@ function greet({ name, greeting }) {
|
|||
greet({ name: 'Larry', greeting: 'Ahoy' })
|
||||
```
|
||||
|
||||
Destructuring of objects and arrays can be also be done in function arguments.
|
||||
|
||||
### Reassigning keys
|
||||
|
||||
```js
|
||||
function printCoordinates({ left: x, top: y }) {
|
||||
console.log(`x: ${x}, y: ${y}`)
|
||||
}
|
||||
```
|
||||
{: data-line="1"}
|
||||
|
||||
```js
|
||||
printCoordinates({ left: 25, top: 90 })
|
||||
```
|
||||
|
||||
This example assigns `x` to the value of the `left` key.
|
||||
|
||||
### Loops
|
||||
|
||||
```js
|
||||
|
|
Loading…
Reference in New Issue