解构赋值
1、数组解构:
允许我们按照一一对应的关系从数组中提取值,然后将值赋值给变量
let [a,b,c] = [1,2,3];
console.log(a);
console.log(b);
console.log(c);
2、对象解构
允许我们使用变量的名字匹配对象的属性,匹配成功将对象属性的值赋值给变量
方法一:let person = {
name: "李四",
age: 30,
sex: "男"
};
let {name,age,sex} = person;
console.log(name);
console.log(age);
console.log(sex);
方法二:let person = {
name: "李四",
age: 30,
sex: "男"
};
let {name:n,age:a,sex:s} = person;
console.log(n);
console.log(a);
console.log(s);