let a;
[a,b]=[1,2]//数组类型解构赋值
console.log(a);//1
[a,b,...rest]=[1,2,3,4,5,6]//输出1,2,[3,4,5,6]
[a,b,c=3]=[1,2] //a=1,b=2,c=3
[a,b,c]=[1,2]//若左右没有配对成功,则会输出undefined。即c为undefined
let a=1;
let b=2;
[a,b]=[b,a];//可以进行交换,输出2,1
function f(){
return [1,2]
}
let a,b;
[a,b]=f();//输出1,2
function f(){
return [1,2,3,4,5]
}
let a,b,c;
[a,,,b]=f();//a,b输出1,4
function f(){
return [1,2,3,4,5]
}
let a,b,c;
[a,...b]=f();//a,b输出1,[2,3,4,5]