//定义变量
let no = 1001
let name = '周杰伦'
let age = 30
let sex = '男'
//定义对象
let stu1 = {
//属性名:属性值
//这里的属性值是上面定义的变量保存的值
no:no,
name:name,
age:age,
sex:sex
}
console.log(stu1);
console.log('-----------------');
//对象的属性名和属性值的标识相同时,可以省略属性值
let stu2 = {
// 是no:no的简写
no,
name,
age,
sex
}
console.log(stu2);
console.log('--------------------------------------');
let stu3 = {
username:'周杰伦',
userage:30,
usersex:'男',
car:{
carName:'奔驰',
carPrice:'100W'
}
}
//过去我们这样写
// let username = stu3.username
// let userage = stu3.userage
let usersex = stu3.usersex
//现在我们这样写(解构赋值)
// usersex:usersex2 表示在解构的时候对变量名进行重命名
let {username,userage,usersex:usersex2} = stu3
console.log(username,userage,usersex,usersex2);
console.log('-----------');
// let {car} = stu3
// let {carName,carPrice} = car
let {car:{carName,carPrice}} = stu3 //这一行代码,最终会编译成下面的两行代码
// let carName = stu3.car.carName
// let carPrice = stu3.car.carPrice
console.log(carName,carPrice);
let arr = [11,22,33,44,55]
// 解构数组中的元素,采用的[]
let [a,b] = arr
console.log(a,b);