ES6里面新增了扩展运算符... 该运算符主要用于函数调用数组
以下:bash
console.log(...[1, 2, 3])函数
function push(array, ...items) { array.push(...items); } 扩展运算符能够看作是rest参数的逆运算,将数组转为逗号隔开的参数序列this
什么是rest参数,其实这个东西在ES6以前咱们也常常用,那就是function内部保存spa
参数的对象argumentsrest
function sayHi() { alert("hello " + arguments[0] + "," + arguments[1]); }code
不过arguments不是数组而是对象,只是经过下标方式访问而已。有了扩展运算符...,咱们能够将参数变得比arguments更加灵活。对象
例如:string
class A {
constructor(...args) {
if (args.length == 3) {
this._x = args[0];
this._y = args[1];
this._z = args[2];
}
else if (args.length == 2) {
this._x = args[0];
this._y = args[1];
this._z = 0;
}
else {
throw TypeError("args error!");
}
}
}
复制代码