按照一定模式从变量中提取值。
数组的解构赋值
let [x, [[y], z]] = [1, [[2], 3]];
// x=1,y=2,z=3
let [x, , y] = [1, 2, 3];
// x=1,y=3
let [x, y, ...z] = [1];
// x=1,y=undefined,z=[]
let [x, y = 2] = [1, undefined];
// x=1,y=2
// 注意:只有在y的值为undefined时,才会使用默认值
let [x = y, y = 1] = []; // ReferenceError: y is not defined
对象的解构赋值
数组的解构赋值由位置决定,对象的解构赋值由属性名决定。
let { x: y } = { x: 1 };
// y=1
// x=undefined
let x;
({ x } = { x: 1 });
字符串的解构赋值
const [one, two, three] = "123";
// one='1',two='2',three='3'
const { length } = "123";
// length=3