// 特性:
1、从数组和对象中提取值,并按照一一对应的方式对变量进行赋值
2、对象进行解构赋值时,变量前后变量的key值需要相对应(即: {a} = {a: 2})
3、在结构多个值时可以给予一个默认值(如:let { a= 2, b} = { a: undefined, b: 2 } // => a= 2 b= 2);但 当为 null 是无法生效的 因为 null !== undefined
4、 解构不成功,为 undefined
// 简单例子
- 对称数组 / 对象
let [a, b, c] = [1, 2, 3];
// 意为: 在数组 [a, b, c] 中,把 [1, 2, 3] 中的值分别赋给 [a, b, c] 中 按照位置对应的 a b c
console.log(a); // 1
console.log(b); // 2
console.log(c); // 3
// 对象
let {d, e} = {name: 'tom', age: 15}
console.log(d, e)
- 非对称数组 / 对象
let [x, y] = [1, 2, 3];
console.log(x); // 1
console.log(y); // 2
let [x, y] = [1];
console.log(x); // 1
console.log(y); // undefind
- 将剩余的数组 / 对象赋给一个变量
let [a, ...b] = [1, 2, 3, 4]
// a: 1 b: [2, 3, 4]
let { c, ...d } = { c: 1, d: 2, e: 3 };
// c: 1 d : {d:2, e: 3}