//arguments
function fn() {
arguments.push('hello word');
console.log(arguments); //报错: arguments.push is not a function at fn
}
fn([1, 2, 3, 4]);
//rest
function fn1(...a) {
a[0].push('hello word');
console.log(a); //[1, 2, 3, 4, 5, 'hello word']
}
fn1([1, 2, 3, 4, 5]);
总结: arguments报错的原因是因为arguments返回的是一个伪数组,它没有数组的属性和方法,因此会出现报错。而rest返回的是一个数组,可以使用数组的属性和方法。
es6中也新增了许多方法,其中Array.from()方法就可以使集合转化为数组集合。
es6中箭头函数是不识别arguments的。所以用rest来取代arguments。