arguments 是一个对应于传递给函数的参数的类数组对象

即函数参数以 数组元素的形式包装在 arguments 对象中.
在所有函数(非箭头函数)中使用

  1. function func1(a, b, c) {
  2. console.log(arguments[0]);
  3. // expected output: 1
  4. console.log(arguments[1]);
  5. // expected output: 2
  6. console.log(arguments[2]);
  7. // expected output: 3
  8. }
  9. func1(1, 2, 3);
  10. // log 1 2 3
  1. /** 箭头函数没有 arguments 对象 */
  2. var f = (e) => {
  3. console.log('箭头函数', e);
  4. console.log('箭头函数', arguments[0]);
  5. }
  6. f(1111)
  7. // 箭头函数 1111
  8. // 箭头函数 {}

arguments对象不是一个 Array 。它类似于 Array,但除了length属性和索引元素之外没有任何Array属性。例如,它没有 pop 方法。但是它可以被转换为一个真正的Array

  1. // 使用 typeof 检查 arguments 类型
  2. console.log(typeof arguments); // object
  3. //使用Array.from()方法或扩展运算符将参数转换为真实数组
  4. var args = Array.from(arguments);
  5. var args = [...arguments];

注意
严格模式下, 剩余参数默认参数解构赋值参数的存在不会改变 arguments对象的行为,但是在非严格模式下就有所不同了。
当非严格模式中的函数没有包含剩余参数默认参数解构赋值,那么arguments对象中的值跟踪参数的值(反之亦然)。
当非严格模式中的函数包含剩余参数默认参数解构赋值,那么arguments对象中的值不会跟踪参数的值(反之亦然)。相反, arguments反映了调用时提供的参数: