解构赋值
let no = 1001
let name = '周杰伦'
let age = 30
let sex = '男'
let str = {
// 属性名:属性值
// 这里的属性值是上面定义的变量保存的值
no: no,
name: name,
age: age,
sex: sex
}
console.log(str);
console.log('----------------------');
let str2 = {
// 是no: no,的简写
no,
name,
age,
sex
}
console.log(str2);
console.log('----------------------');
let str3 = {
username: '周杰伦',
userage: 30,
usersex: '男',
car: {
carName: '奔驰',
carPrice: '100W'
}
}
// 解构赋值
//过去我们这样写
// let username = stu3.username
// let userage = stu3.userage
// let usersex = stu3.usersex
// 现在我们这样写(解构赋值)
let { username, userage, usersex } = str3
console.log(username, userage, usersex);
let { car: { carName, carPrice } } = str3
console.log(carName, carPrice);
console.log('---------------------------');
let arr = [11, 22, 33, 44, 55]
// 解构数组中的元素,采用[]
let [a, b] = arr
console.log(a, b);