解构赋值
1、解构赋值:解析某一数据的结构,将想要的东西提取出来
2、数组解构赋值:模式匹配+索引值相同的完成赋值
const [a,[, , b],c]=[1,[1,2,3],4]
console.log(a,b,c)结果为1,3,4
3、数组解构默认值
const[a,b]=[]等价于const[a,b]=[undefined,undefined]
只有当一个数组成员严格等于undefined时,对应的默认值才会生效
const[a=1,b=2]=[]
console.log(a,b)
结果为1,2
注意:如果默认值是表达式,惰性求值,只有当书组成员等于undefined时,才会执行表达式
4、对象解构赋值
模式匹配+属性名相同的完成赋值
const{age:age,username:username}={age:1,username:xioaxiao}
console.log(age,username)
结果为1,xioaxiao
5、其它数据类型的解构赋值
字符串的解构赋值:
数组形式接收
const[a,b]='hello'; console.log(a,b)
对象形式解构赋值
const({0:a,1:b})='hello'
结果为h,e
数字和布尔值的解构赋值,会将右边的值转换为对象
undefined和null进行解构赋值会报错。

浙公网安备 33010602011771号