扩展运算符
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" ]
扩展运算符( spread )是三个点(...)。它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。
{
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 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构
var tools = {
add:function (){},
isFunction: function (params){
console.log(params);
},
isArray:function (){
},
a: 'leo'
}
设置别名
let {a:name='miaov'} = tools;//原来的名字不可用,
设置默认值
let {isLee=123} = tools
数组解构赋值
var arr = ['miaov','ketang','leo'];
var [a,b,c] = arr;
console.log(a,b,c); // miaov ketang leo
网友评论