es6基础入门变量的解构赋值

1 let [a, b, c] = [1, 2, 3];
 1 let [foo, [[bar], baz]] = [1, [[2], 3]];
 2 foo // 1
 3 bar // 2
 4 baz // 3
 5 
 6 let [ , , third] = ["foo", "bar", "baz"];
 7 third // "baz"
 8 
 9 let [x, , y] = [1, 2, 3];
10 x // 1
11 y // 3
12 
13 let [head, ...tail] = [1, 2, 3, 4];
14 head // 1
15 tail // [2, 3, 4]
16 
17 let [x, y, ...z] = ['a'];
18 x // "a"
19 y // undefined
20 z // []
1 // 报错
2 let [foo] = 1;
3 let [foo] = false;
4 let [foo] = NaN;
5 let [foo] = undefined;
6 let [foo] = null;
7 let [foo] = {};
1 let [x, y, z] = new Set(['a', 'b', 'c']);
2 x // "a"
1 let [foo = true] = [];
2 foo // true
3 
4 let [x, y = 'b'] = ['a']; // x='a', y='b'
5 let [x, y = 'b'] = ['a', undefined]; // x='a', y='b'
1 let [x = 1] = [undefined];
2 x // 1
3 
4 let [x = 1] = [null];
5 x // null
1 let [x = 1, y = x] = [];     // x=1; y=1
2 let [x = 1, y = x] = [2];    // x=2; y=2
3 let [x = 1, y = x] = [1, 2]; // x=1; y=2
4 let [x = y, y = 1] = [];     // ReferenceError
1 let { foo, bar } = { foo: "aaa", bar: "bbb" };
2 foo // "aaa"
3 bar // "bbb"
1 let { bar, foo } = { foo: "aaa", bar: "bbb" };
2 foo // "aaa"
3 bar // "bbb"
4 
5 let { baz } = { foo: "aaa", bar: "bbb" };
6 baz // undefined
1 var { foo: baz } = { foo: 'aaa', bar: 'bbb' };
2 baz // "aaa"
3 
4 let obj = { first: 'hello', last: 'world' };
5 let { first: f, last: l } = obj;
6 f // 'hello'
7 l // 'world'
1 let { foo: baz } = { foo: "aaa", bar: "bbb" };
2 baz // "aaa"
3 foo // error: foo is not defined

 

posted on 2017-07-21 17:02  -韩帅  阅读(172)  评论(0编辑  收藏  举报

导航