arguments 是一个对应于传递给函数的参数的类数组对象
即函数参数以 数组元素的形式包装在 arguments 对象中.
在所有函数(非箭头函数)中使用
function func1(a, b, c) {console.log(arguments[0]);// expected output: 1console.log(arguments[1]);// expected output: 2console.log(arguments[2]);// expected output: 3}func1(1, 2, 3);// log 1 2 3
/** 箭头函数没有 arguments 对象 */var f = (e) => {console.log('箭头函数', e);console.log('箭头函数', arguments[0]);}f(1111)// 箭头函数 1111// 箭头函数 {}
arguments对象不是一个 Array 。它类似于 Array,但除了length属性和索引元素之外没有任何Array属性。例如,它没有 pop 方法。但是它可以被转换为一个真正的Array
// 使用 typeof 检查 arguments 类型console.log(typeof arguments); // object//使用Array.from()方法或扩展运算符将参数转换为真实数组var args = Array.from(arguments);var args = [...arguments];
注意
严格模式下, 剩余参数、默认参数和解构赋值参数的存在不会改变 arguments对象的行为,但是在非严格模式下就有所不同了。
当非严格模式中的函数没有包含剩余参数、默认参数和解构赋值,那么arguments对象中的值会跟踪参数的值(反之亦然)。
当非严格模式中的函数有包含剩余参数、默认参数和解构赋值,那么arguments对象中的值不会跟踪参数的值(反之亦然)。相反, arguments反映了调用时提供的参数:
