Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Answer JS destructuring question #14

Merged
merged 2 commits into from
Feb 13, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update README.md
  • Loading branch information
yangshun authored Feb 13, 2018
commit 35aae0559ceceba54c1dac8ae8eeadbcef834c24
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1567,10 +1567,9 @@ Use **higher-order function** to make your code easy to reason about and improve

### Can you give an example for destructuring an object or an array?

Destructuring is an expression available in ES6 which enables a succint and convenient way to extract values of Objects or Arrays,
and place them into distinct variables.
Destructuring is an expression available in ES6 which enables a succinct and convenient way to extract values of Objects or Arrays, and place them into distinct variables.

Basic Array destructuring:
**Array destructuring**

```js
// Variable assignment
Expand All @@ -1583,34 +1582,36 @@ console.log(three); // "three"
```
```js
// Swapping variables
var a = 1;
var b = 3;
const a = 1;
const b = 3;

[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1

```
Basic Object destructuring:

**Object destructuring**

```js
// Variable assignment
var o = {p: 42, q: true};
var {p, q} = o;
const o = {p: 42, q: true};
const {p, q} = o;

console.log(p); // 42
console.log(q); // true

```
```js
// Assignment without declaration
var a, b;
let a, b;

({a, b} = {a: 1, b: 2});

```

###### References

* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
* https://ponyfoo.com/articles/es6-destructuring-in-depth

Expand Down