解构就是模式匹配
譬如
let [a,b,c,d]=[true,'zifuchuan',2]
//相当于a=true,b='zifuchuan',c=2,d=undefined
作用
- 从数组中取值,得不到则给一个默认值
let [x=2]=arr//x匹配arr数组的第一个元素,得不到则赋值为2
注意 null可以赋值成功,undefined则可以得到默认值
let [x=2]=[null]//则x=null
let [y=3]=[undefiend]//则y=3
微信扫一扫
譬如
let [a,b,c,d]=[true,'zifuchuan',2]
//相当于a=true,b='zifuchuan',c=2,d=undefined
let [x=2]=arr//x匹配arr数组的第一个元素,得不到则赋值为2
let [x=2]=[null]//则x=null
let [y=3]=[undefiend]//则y=3
相关推荐