es6剩余参数…拓展应用

函数传参

function restParam(a,b,...c){};//b以后所有参数放入c数组中
//c必须为最后一个参数

拓展应用

fn(...[1,2,3])等同于fn(1,2,3)
  • 级联更容易
[1,2,...[3,4,5],6,7]; //1,2,3,4,5,6,7
  • 可迭代对象转为数组
[...document.querySelectorAll('img')];//转为数组
  • 解构时也可用
[a,,...rest]=[1,2,3,4,5];//rest为[3,4,5]
  • 初始化对象时传参毫不费力
new Date(...[2015,31,5])

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

CAPTCHAis initialing...
退出移动版