Function.prototype.arguments

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Non-standard: This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

注意:Function 对象的 arguments 属性已弃用。访问 arguments 对象的推荐方法是引用函数内可用的变量 arguments

¥Note: The arguments property of Function objects is deprecated. The recommended way to access the arguments object is to refer to the variable arguments available within functions.

Function 实例的 arguments 访问器属性返回传递给该函数的参数。对于 strict、箭头、异步和生成器函数,访问 arguments 属性会抛出 TypeError

¥The arguments accessor property of Function instances returns the arguments passed to this function. For strict, arrow, async, and generator functions, accessing the arguments property throws a TypeError.

描述

¥Description

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

¥The value of arguments is an array-like object corresponding to the arguments passed to a function.

在递归的情况下,即如果函数 f 在调用堆栈上出现多次,则 f.arguments 的值表示与该函数的最近调用相对应的参数。

¥In the case of recursion, i.e. if function f appears several times on the call stack, the value of f.arguments represents the arguments corresponding to the most recent invocation of the function.

如果正在进行的函数没有未完成的调用(即函数已被调用但尚未返回),则 arguments 属性的值通常为 null

¥The value of the arguments property is normally null if there is no outstanding invocation of the function in progress (that is, the function has been called but has not yet returned).

请注意,ECMAScript 规范指定的唯一行为是 Function.prototype 有一个初始 arguments 访问器,它无条件地为任何 getset 请求(称为 "毒丸访问器")抛出 TypeError,并且不允许实现更改任何函数的此语义,除了 非严格的普通函数。arguments 属性的实际行为(如果不是抛出错误)是由实现定义的。例如,Chrome 将其定义为自己的数据属性,而 Firefox 和 Safari 扩展了初始毒丸 Function.prototype.arguments 访问器以专门处理非严格函数的 this 值。

¥Note that the only behavior specified by the ECMAScript specification is that Function.prototype has an initial arguments accessor that unconditionally throws a TypeError for any get or set request (known as a "poison pill accessor"), and that implementations are not allowed to change this semantic for any function except non-strict plain functions. The actual behavior of the arguments property, if it's anything other than throwing an error, is implementation-defined. For example, Chrome defines it as an own data property, while Firefox and Safari extend the initial poison-pill Function.prototype.arguments accessor to specially handle this values that are non-strict functions.

js
(function f() {
  if (Object.hasOwn(f, "arguments")) {
    console.log(
      "arguments is an own property with descriptor",
      Object.getOwnPropertyDescriptor(f, "arguments"),
    );
  } else {
    console.log(
      "f doesn't have an own property named arguments. Trying to get f.[[Prototype]].arguments",
    );
    console.log(
      Object.getOwnPropertyDescriptor(
        Object.getPrototypeOf(f),
        "arguments",
      ).get.call(f),
    );
  }
})();

// In Chrome:
// arguments is an own property with descriptor {value: Arguments(0), writable: false, enumerable: false, configurable: false}

// In Firefox:
// f doesn't have an own property named arguments. Trying to get f.[[Prototype]].arguments
// Arguments { … }

示例

¥Examples

使用参数属性

¥Using the arguments property

js
function f(n) {
  g(n - 1);
}

function g(n) {
  console.log(`before: ${g.arguments[0]}`);
  if (n > 0) {
    f(n);
  }
  console.log(`after: ${g.arguments[0]}`);
}

f(2);

console.log(`returned: ${g.arguments}`);

// Logs:
// before: 1
// before: 0
// after: 0
// after: 1
// returned: null

规范

¥Specifications

不属于任何标准。

¥Not part of any standard.

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also