arguments
是一个伪(类)数组,指的是函数的参数集合,不能调用数组的方法。
1.arguments的特点:
①.只在函数中存在(箭头函数除外)。
②.有长度length,可通过下标index访问。
③.是一个集合,存储了我们传入的所有实参。
2.arguments方法(转换成真的数组可以使用个数组的方法):
①.展开运算符:
function fn (){
let arr = [...arguments];
// arr 就是一个数组
}
fn(1,2,3)
②.call()方法:
function fn (){
Array.prototype.push.call(arguments,4,5)
// 借用 数组的方法 末尾添加一个元素
console.log(arguments);
};
fn(1,2,3)
③.Array.prototype.slice.apply(arguments)
function test(){
//将参数转为一个数组
var args = Array.prototype.slice.apply(arguments);
alert(args);
}