什么是解构
解构是es6的一种语法规则,将一个对象或数组的某个属性提取到某个变量中
注意
解构不会对被解构的目标造成任何影响
对象解构语法
1.同名属性
// var/let/const{属性名}=被解构的对象
const user = {
name: "abc",
age: 18,
sex: "男",
address: {
province: "重庆",
city: "重庆"
}
}
let {
name, age, sex, address} = user;
console.log(name, age, sex, address);
也可以多添加一个属性
const user = {
name: "abc",
age: 18,
sex: "男",
address: {
province: "重庆",
city: "重庆"
}
}
let {
name, age, sex, address, a=123} = user;
console.log(name, age, sex, address, a);
2.非同名属性
// var/let/const{属性名:变量名}=被解构的对象
const user = {
name: "abc",
age: 18,
sex: "男",
address: {
province: "重庆",
city: "重庆"
}
}
let {
name, age, sex: gender, address