forked from adamszaruga/es6
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdemo.js
43 lines (32 loc) · 1.1 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// You're already used to "packing" things into new arrays and objects, like so:
let array = [ 1, 2, "foo"];
let object = {
key1: "hello",
key2: "goodbye"
}
// With ES6 destructuring, you're now able to "unpack" arrays and objects into separate variables:
let [ first, second, third ] = array;
first === 1;
second === 2;
third === "foo";
let { key1, key2 } = object;
key1 === "hello";
key2 === "goodbye";
// It's still possible to "unpack" arrays and objects without
// destructuring, but you would need a new line for each unpacked variable:
let first = array[0];
let second = array[1];
let third = array[2];
let key1 = object.key1;
let key2 = object.key2;
// With array destructuring, you can skip values that you're not interested in:
let [one, , three] = array;
one === 1;
three === "foo";
// With object destructuring, you can rename variable names using a colon:
let { key1: firstKey } = object;
firstKey === "hello";
// Also, if a value doesn't exist, you can provide a fallback value in your destructuring assignment:
let { key1 = "hi", key3 = "sup"} = object;
key1 === "hello";
key3 === "sup";