擴(kuò)展運(yùn)算符
console.log(...[1,2,3]);//1,2,3
console.log(1,...[2,3,4],5);//1,2,3,4,5
console.log(...document.querySelectorAll("div")) //<div>1</div><div>1</div>
console.log([...'hello']); //[ "h", "e", "l", "l", "o" ]
擴(kuò)展運(yùn)算符( spread )是三個(gè)點(diǎn)(...)准浴。它好比 rest 參數(shù)的逆運(yùn)算事扭,將一個(gè)數(shù)組轉(zhuǎn)為用逗號(hào)分隔的參數(shù)序列。
{
let a,b,rest;
[a,b,...rest]=[1,2,3,4,5,6]
console.log(a,b,rest); //1 2 [3,4,5,6]
}
ES6 允許按照一定模式乐横,從數(shù)組和對(duì)象中提取值求橄,對(duì)變量進(jìn)行賦值,這被稱為解構(gòu)
var tools = {
add:function (){},
isFunction: function (params){
console.log(params);
},
isArray:function (){
},
a: 'leo'
}
設(shè)置別名
let {a:name='miaov'} = tools;//原來(lái)的名字不可用葡公,
設(shè)置默認(rèn)值
let {isLee=123} = tools
數(shù)組解構(gòu)賦值
var arr = ['miaov','ketang','leo'];
var [a,b,c] = arr;
console.log(a,b,c); // miaov ketang leo