多種解構(gòu)賦值
1.解構(gòu)賦值對象
let {a,b}={a:1,b:2}
//a=1,b=2,//屬性一致
let {a:c}={a:1,b:2}
//屬性不一致,新的變量名
c=1,a=undefined
//對象的解構(gòu)是沒有次序的,也就是說,對象的解構(gòu)賦值的內(nèi)部機制,是先找到同名屬性,然后再賦給對應(yīng)的變量。真正被賦值的是后者,而不是前者。
2.解構(gòu)賦值數(shù)組
//數(shù)組的解構(gòu)是有次序的
let [,,c]=[1,2,3]
//c=3
let [a,...b]=[1,2,3,4];
//a=1;
//b=[2,3,4]
let [a,b,...c]=[1];
//a=1;
//b=undefined;如果解構(gòu)不成功,變量的值就等于undefined。
//c=[];
不完全解構(gòu),即等號左邊的模式,只匹配一部分的等號右邊的數(shù)組。
let [x, y] = [1,2,3];
x// 1y// 2
let [a,[b], d] = [1,[2,3],4];
a// 1
b// 2
d // 4
3.解構(gòu)賦值函參
var func=([x,y])=>console.debug(x,y);
func([1,2]);//1,2
func([1]);//1,undefined
func([]);//undefined,undefined
var func=({x,y:yy})=>{
? ? ? ? console.debug(x,y,yy);
? ? }
? ? func({x:1,y=2});//1,undefined,2
? ? func({y:2});//undefines,undefined,2
? ? func({});//undefined,uindefined,undefined
//三個點(...) 有2個含義。ES6中擴展運算符(spread)和剩余運算符(rest)
4.rest參數(shù)(與。。。相反)===聲明就是剩余運算符
5.。。。(與rest相反)轉(zhuǎn)為用逗號分隔的參數(shù)序列。(對象和數(shù)組都適用)==調(diào)用直接使用就是擴展運算符
6.函數(shù)參數(shù)與剩余運算符
7.函數(shù)參數(shù)與擴展運算符()序列,數(shù)組,對象
let func=(a,b,c)=>console.debug(c);
? ? var arr=[1,2,3]
? ? func(...arr);
//3
let foo=(a,...b)=>console.debug(a,b);
? ? foo(...arr);
//1,[2,3]