参数对象

arguments 是一个可在 functions 内部访问的类似数组的对象,其中包含传递给该函数的参数值。

¥**arguments** is an array-like object accessible inside functions that contains the values of the arguments passed to that function.

Try it

描述

¥Description

注意:在现代代码中,其余参数 应该是首选。

¥Note: In modern code, rest parameters should be preferred.

arguments 对象是所有非 arrow 函数中可用的局部变量。你可以使用函数的 arguments 对象来引用该函数内部的函数参数。它具有调用函数所用的每个参数的条目,第一个条目的索引位于 0

¥The arguments object is a local variable available within all non-arrow functions. You can refer to a function's arguments inside that function by using its arguments object. It has entries for each argument the function was called with, with the first entry's index at 0.

例如,如果一个函数传递了 3 个参数,你可以按如下方式访问它们:

¥For example, if a function is passed 3 arguments, you can access them as follows:

js
arguments[0]; // first argument
arguments[1]; // second argument
arguments[2]; // third argument

arguments 对象对于使用比正式声明接受的参数多的参数调用的函数很有用,称为 可变参数函数,例如 Math.min()。此示例函数接受任意数量的字符串参数并返回最长的一个:

¥The arguments object is useful for functions called with more arguments than they are formally declared to accept, called variadic functions, such as Math.min(). This example function accepts any number of string arguments and returns the longest one:

js
function longestString() {
  let longest = "";
  for (let i = 0; i < arguments.length; i++) {
    if (arguments[i].length > longest.length) {
      longest = arguments[i];
    }
  }
  return longest;
}

你可以使用 arguments.length 来计算调用该函数时使用的参数数量。如果你想计算函数声明接受的参数数量,请检查该函数的 length 属性。

¥You can use arguments.length to count how many arguments the function was called with. If you instead want to count how many parameters a function is declared to accept, inspect that function's length property.

分配给索引

¥Assigning to indices

每个参数索引也可以设置或重新分配:

¥Each argument index can also be set or reassigned:

js
arguments[1] = "new value";

仅具有简单参数(即没有剩余参数、默认参数或解构参数)的非严格函数会将参数的新值与 arguments 对象同步,反之亦然:

¥Non-strict functions that only have simple parameters (that is, no rest, default, or destructured parameters) will sync the new value of parameters with the arguments object, and vice versa:

js
function func(a) {
  arguments[0] = 99; // updating arguments[0] also updates a
  console.log(a);
}
func(10); // 99

function func2(a) {
  a = 99; // updating a also updates arguments[0]
  console.log(arguments[0]);
}
func2(10); // 99

传递 restdefaultdestructured 参数的非严格函数不会将分配给函数体中的参数的新值与 arguments 对象同步。相反,具有复杂参数的非严格函数中的 arguments 对象将始终反映调用函数时传递给函数的值。

¥Non-strict functions that are passed rest, default, or destructured parameters will not sync new values assigned to parameters in the function body with the arguments object. Instead, the arguments object in non-strict functions with complex parameters will always reflect the values passed to the function when the function was called.

js
function funcWithDefault(a = 55) {
  arguments[0] = 99; // updating arguments[0] does not also update a
  console.log(a);
}
funcWithDefault(10); // 10

function funcWithDefault2(a = 55) {
  a = 99; // updating a does not also update arguments[0]
  console.log(arguments[0]);
}
funcWithDefault2(10); // 10

// An untracked default parameter
function funcWithDefault3(a = 55) {
  console.log(arguments[0]);
  console.log(arguments.length);
}
funcWithDefault3(); // undefined; 0

所有 严格模式函数 都表现出相同的行为,无论它们传递的参数类型如何。也就是说,为函数体中的参数分配新值永远不会影响 arguments 对象,为 arguments 索引分配新值也不会影响参数的值,即使函数只有简单参数也是如此。

¥This is the same behavior exhibited by all strict-mode functions, regardless of the type of parameters they are passed. That is, assigning new values to parameters in the body of the function never affects the arguments object, nor will assigning new values to the arguments indices affect the value of parameters, even when the function only has simple parameters.

注意:你不能在接受剩余参数、默认参数或解构参数的函数定义主体中编写 "use strict"; 指令。这样做会抛出 语法错误

¥Note: You cannot write a "use strict"; directive in the body of a function definition that accepts rest, default, or destructured parameters. Doing so will throw a syntax error.

参数是一个类似数组的对象

¥arguments is an array-like object

arguments 是一个类似数组的对象,这意味着 arguments 具有 length 属性和从零索引的属性,但它没有 Array 的内置方法,如 forEach()map()。但是,可以使用 slice()Array.from()扩展语法 之一将其转换为真正的 Array

¥arguments is an array-like object, which means that arguments has a length property and properties indexed from zero, but it doesn't have Array's built-in methods like forEach() or map(). However, it can be converted to a real Array, using one of slice(), Array.from(), or spread syntax.

js
const args = Array.prototype.slice.call(arguments);
// or
const args = Array.from(arguments);
// or
const args = [...arguments];

对于常见用例,将其用作类似数组的对象就足够了,因为它既有 是可迭代的,又有 length 和数字索引。例如,Function.prototype.apply() 接受类似数组的对象。

¥For common use cases, using it as an array-like object is sufficient, since it both is iterable and has length and number indices. For example, Function.prototype.apply() accepts array-like objects.

js
function midpoint() {
  return (
    (Math.min.apply(null, arguments) + Math.max.apply(null, arguments)) / 2
  );
}

console.log(midpoint(3, 1, 4, 1, 5)); // 3

属性

¥Properties

arguments.callee Deprecated

引用参数所属的当前正在执行的函数。严格模式下禁止。

arguments.length

传递给函数的参数数量。

arguments[Symbol.iterator]()

返回一个新的 Array iterator 对象,其中包含 arguments 中每个索引的值。

示例

¥Examples

定义一个连接多个字符串的函数

¥Defining a function that concatenates several strings

此示例定义了一个连接多个字符串的函数。该函数的唯一形式参数是一个字符串,其中包含分隔要连接的项的字符。

¥This example defines a function that concatenates several strings. The function's only formal argument is a string containing the characters that separate the items to concatenate.

js
function myConcat(separator) {
  const args = Array.prototype.slice.call(arguments, 1);
  return args.join(separator);
}

你可以向此函数传递任意数量的参数。它使用列表中的每个参数返回一个字符串列表:

¥You can pass as many arguments as you like to this function. It returns a string list using each argument in the list:

js
myConcat(", ", "red", "orange", "blue");
// "red, orange, blue"

myConcat("; ", "elephant", "giraffe", "lion", "cheetah");
// "elephant; giraffe; lion; cheetah"

myConcat(". ", "sage", "basil", "oregano", "pepper", "parsley");
// "sage. basil. oregano. pepper. parsley"

定义创建 HTML 列表的函数

¥Defining a function that creates HTML lists

此示例定义了一个函数,该函数为列表创建包含 HTML 的字符串。该函数的唯一形式参数是一个字符串,如果列表为 无序(项目符号),则为 "u";如果列表为 已排序(已编号),则为 "o"。该函数定义如下:

¥This example defines a function that creates a string containing HTML for a list. The only formal argument for the function is a string that is "u" if the list is to be unordered (bulleted), or "o" if the list is to be ordered (numbered). The function is defined as follows:

js
function list(type) {
  let html = `<${type}l><li>`;
  const args = Array.prototype.slice.call(arguments, 1);
  html += args.join("</li><li>");
  html += `</li></${type}l>`; // end list
  return html;
}

你可以向此函数传递任意数量的参数,它会将每个参数作为列表项添加到指定类型的列表中。例如:

¥You can pass any number of arguments to this function, and it adds each argument as a list item to a list of the type indicated. For example:

js
list("u", "One", "Two", "Three");
// "<ul><li>One</li><li>Two</li><li>Three</li></ul>"

将 typeof 与参数一起使用

¥Using typeof with arguments

arguments 一起使用时,typeof 运算符返回 'object'

¥The typeof operator returns 'object' when used with arguments

js
console.log(typeof arguments); // 'object'

各个参数的类型可以通过索引 arguments 来确定:

¥The type of individual arguments can be determined by indexing arguments:

js
console.log(typeof arguments[0]); // returns the type of the first argument

规范

Specification
ECMAScript Language Specification
# sec-arguments-exotic-objects

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also