es5中参数不确定个数的情况下:
//求参数和
function f(){
var a = Array.prototype.slice.call(arguments);
var sum = 0;
a.forEach(function(item){
sum += item*1;
})
return sum;
};
f(1,2,3);//6
es6中可变参数:
function f(...a){
let sum = 0;
a.forEach(item =>{
sum += item*1;
})
return sum;
}
f(1,2,3);//6
...a 为扩展运算符,这个 a 表示的就是可变参数的列表,为一个数组
合并数组
//es5 var param = ['hello',true,7]; var other = [1,2].concat(param); console.log(other);//[1, 2, "hello", true, 7]
//es6 var param = ['hello',true,7]; var other = [1,2,...param]; console.log(other);// [1, 2, "hello", true, 7]
本文对比了ES5中使用`arguments`处理不定数量参数的方法与ES6引入的可变参数`...a`。讲解了如何利用扩展运算符合并数组,并展示了不同版本在处理函数参数上的进步。
722

被折叠的 条评论
为什么被折叠?



