1.入门小案例
var arr=[1,2,3];
var [a,b,c]=arr;
console.log(a,b,c) //1,2,3
2.多维数组解构赋值
let arr=[22,[5,3],19]
let [a,[b,c],d]=arr
console.log(a,b,c,d)//22,5,3,19
3.变量交换
let x=11;
let y=22;
[y,x]=[x,y]
console.log(x,y) //22,11
4.不完全解析
let arr=[22,[5,3],19]
let [a,[b],d]=arr
console.log(a,b,d)//22,5,19
let [a,[,c],d]=arr
console.log(a,c,d)//22,3,19
5.不可解析
let [x,y]=NaN//undefined ,null ,{}
6.Set解析
let [x,y]=new Set([111,222]);
console.log(x,y) //111,222
7.所有实现了iterator的数据结构都能被数组解析
class Group{
constructor(){
}
next(){
return {value:'ggb',done:false}
}
[Symbol.iterator](){
return this;
}
}
let group = new Group();
let [x,y]=group;
console.log(x,y)//ggb,ggb
8.三个点运算赋
var [x,y,...z]=[1,2,3,4,5,6]
console.log(x,y,z)//1 2 [ 3, 4, 5, 6 ]