Function.prototype.arguments

已弃用: 不再推荐使用该特性。虽然一些浏览器仍然支持它,但也许已从相关的 web 标准中移除,也许正准备移除或出于兼容性而保留。请尽量不要使用该特性,并更新现有的代码;参见本页面底部的兼容性表格以指导你作出决定。请注意,该特性随时可能无法正常工作。

非标准: 该特性是非标准的,请尽量不要在生产环境中使用它!

备注: Function 对象的 arguments 属性已被弃用。访问 arguments 对象的推荐方式是在函数内部引用可用的 arguments 变量。

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

描述

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

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

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

请注意,ECMAScript 规范只规定了 Function.prototype 具有一个初始的 arguments 访问器,对于任何 getset 请求都无条件地抛出 TypeError(称为“毒丸访问器”),而且引擎实现不允许改变此语义,除非是非严格的普通函数。(对于非严格的普通函数)arguments 属性的实际行为如果不是抛出错误,则该行为由实现定义。例如,Chrome 将其定义为自有的数据属性,而 Firefox 和 Safari 扩展了初始的毒丸访问器 Function.prototype.arguments,以特殊处理非严格函数的 this 值。

js
(function f() {
  if (Object.hasOwn(f, "arguments")) {
    console.log(
      "arguments 是带有描述符的自有属性",
      Object.getOwnPropertyDescriptor(f, "arguments"),
    );
  } else {
    console.log(
      "f 没有自有的名为 arguments 的属性。尝试获取 f.[[Prototype]].arguments",
    );
    console.log(
      Object.getOwnPropertyDescriptor(
        Object.getPrototypeOf(f),
        "arguments",
      ).get.call(f),
    );
  }
})();

// 在 Chrome 中:
// arguments 是带有描述符的自有属性 {value: Arguments(0), writable: false, enumerable: false, configurable: false}

// 在 Firefox 中:
// f 没有自有的名为 arguments 的属性。尝试获取 f.[[Prototype]].arguments
// Arguments { … }

示例

使用 arguments 属性

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

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

f(2);

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

// 输出:
// 之前:1
// 之前:0
// 之后:0
// 之后:1
// 返回:null

规范

不属于任何规范。

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
arguments
DeprecatedNon-standard

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Non-standard. Check cross-browser support before using.
Deprecated. Not for use in new websites.

参见