From 27a61fea1af492ac787844df58aab66c593ce374 Mon Sep 17 00:00:00 2001 From: Ahsan Ayaz Date: Sun, 22 Oct 2017 16:28:04 +0500 Subject: [PATCH 1/3] added desctructures with defaults for array & object \nAdded examples of function arguments destructring --- es6.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/es6.md b/es6.md index 4c21a5fa4..180b9c706 100644 --- a/es6.md +++ b/es6.md @@ -189,6 +189,17 @@ Destructuring var [first, last] = ['Nikola', 'Tesla'] ``` +Default values can be assigned while destructuring arrays +```js +var subjectScores = [22, 33]; +var [maths = 50, physics = 50, chemistry = 50, ics] = subjectScores; + +/* + results in: + maths === 22, physics === 33, chemistry === 50, ics === undefined +*/ +``` + #### Objects ```js @@ -197,6 +208,18 @@ let {title, author} = { author: 'R. Galbraith' } ``` +Default values can be assigned while destructuring objects +```js +var obj = {a : 1}; +var {a, b = 5} = obj; + +/* + results in: + a === 1 + b === 5 +*/ +``` + {: data-line="1"} Supports for matching arrays and objects. @@ -209,11 +232,21 @@ See: [Destructuring](http://babeljs.io/docs/learn-es2015/#destructuring) function greet({ name, greeting }) { console.log(`${greeting}, ${name}!`) } + +function add([ num1, num2 ]) { + console.log(`result: ${num1 + num2}`) +} + +function printCoordinates({ coordx: x, coordy: y }) { + console.log(`x: ${x}, y: ${y}`); +} ``` {: data-line="1"} ```js greet({ name: 'Larry', greeting: 'Ahoy' }) +add([2,4]) +printCoordinates({ coordx: 25, coordy: 90 }) ``` ### Loops From ea81bd0502a2a24ffb78b606febc0033a02637e9 Mon Sep 17 00:00:00 2001 From: "Rico Sta. Cruz" Date: Thu, 26 Oct 2017 14:17:30 +0800 Subject: [PATCH 2/3] es6: Update destructuring assignment defaults example --- es6.md | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/es6.md b/es6.md index 180b9c706..7bf12fb52 100644 --- a/es6.md +++ b/es6.md @@ -189,17 +189,6 @@ Destructuring var [first, last] = ['Nikola', 'Tesla'] ``` -Default values can be assigned while destructuring arrays -```js -var subjectScores = [22, 33]; -var [maths = 50, physics = 50, chemistry = 50, ics] = subjectScores; - -/* - results in: - maths === 22, physics === 33, chemistry === 50, ics === undefined -*/ -``` - #### Objects ```js @@ -208,23 +197,25 @@ let {title, author} = { author: 'R. Galbraith' } ``` -Default values can be assigned while destructuring objects -```js -var obj = {a : 1}; -var {a, b = 5} = obj; - -/* - results in: - a === 1 - b === 5 -*/ -``` - {: data-line="1"} Supports for matching arrays and objects. See: [Destructuring](http://babeljs.io/docs/learn-es2015/#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 From ec9debe80f128aaa62d1d4b7ee2ae123231bfe39 Mon Sep 17 00:00:00 2001 From: "Rico Sta. Cruz" Date: Thu, 26 Oct 2017 14:20:56 +0800 Subject: [PATCH 3/3] es6: Move reassigning keys to its own example --- es6.md | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/es6.md b/es6.md index 7bf12fb52..1b9bc7f95 100644 --- a/es6.md +++ b/es6.md @@ -223,23 +223,30 @@ Default values can be assigned while destructuring arrays or objects. function greet({ name, greeting }) { console.log(`${greeting}, ${name}!`) } - -function add([ num1, num2 ]) { - console.log(`result: ${num1 + num2}`) -} - -function printCoordinates({ coordx: x, coordy: y }) { - console.log(`x: ${x}, y: ${y}`); -} ``` {: data-line="1"} ```js greet({ name: 'Larry', greeting: 'Ahoy' }) -add([2,4]) -printCoordinates({ coordx: 25, coordy: 90 }) ``` +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