一、 数组的解构赋值
let arr = [1, 2, 3];
let [a, b, c] = arr;
console.log(a, b, c); //1,2,3
//模式匹配解构赋值
let [foo, [[bar], baz]] = [1, [[2], 3]];
console.log(foo, bar, baz); //1,2,3
//省略解构赋值
let [, , e, , f] = [1, 2, 3, 4, 5];
console.log(e, f); //3,5
//含剩余参数的解构赋值
let [g, ...reset] = [1, 2, 3, 4, 5];
console.log(g, reset); //1,[2,3,4,5]
二、对象的解构赋值
let p1 = {
name: "zhangsan",
age: 25,
};
let { name, age } = p1; //注意变量必须为属性名
console.log(name, age); //"zhangsan",25
三、字符串的解构赋值
const [a, b, c, d, e] = "hello";
console.log(a, b, c, d, e); //'h','e','l','l','o'