一、arguments是什么?
arguments是所有函数都具有的一个内置局部变量,表示的是函数实际接收的参数对象。arguments是一个类数组对象,之所以称为类数组对象,是因为他除了数组所具有的length属性外,并没有数组本身自带的其他方法。
二、arguments对象详解
1.arguments的使用
//arguments对象
getArguments(a: any, b: any) {
console.log(arguments);
const arr = [...arguments];//可以将类数组转换为数组获得含有实参的数组
console.log(arr); //[12,11]
console.log(arguments[0]); //12
console.log(arguments[1]); //11
console.log(arguments[2]); //undefined
//指定的形参传递值的情况下,arguments对象与形参值相同,并且可以相互改变。
arguments[0] = 1;
console.log(arguments);
}
//调用该函数并传入参数
this.getArguments(12, 11);
输出为:
实参在arguments类数组中
2.指定的形参传递值的情况下,arguments对象与形参值相同,并且可以相互改变。
//arguments对象
getArguments(a: any, b: any) {
//指定的形参传递值的情况下,arguments对象与形参值相同,并且可以相互改变。
arguments[0] = 1;
console.log(arguments);
}
//调用该函数并传入参数
this.getArguments(12, 11);
输出为: