参考错误:已弃用的调用者或参数用法

当使用已弃用的 Function.prototype.callerFunction.prototype.arguments 属性时,会发生 JavaScript 严格模式-only 异常 "已弃用的调用者或参数用法"。

¥The JavaScript strict mode-only exception "deprecated caller or arguments usage" occurs when the deprecated Function.prototype.caller or Function.prototype.arguments properties are used.

信息

¥Message

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them (V8-based & Firefox)
TypeError: 'arguments', 'callee', and 'caller' cannot be accessed in this context. (Safari)

错误类型

¥Error type

仅限 严格模式 中的 TypeError

¥TypeError in strict mode only.

什么地方出了错?

¥What went wrong?

严格模式 中,使用了 Function.prototype.callerFunction.prototype.arguments 属性,但不应该使用。它们已被弃用,因为它们泄漏了函数调用者,是非标准的,难以优化并且可能是一个对性能有害的功能。

¥In strict mode, the Function.prototype.caller or Function.prototype.arguments properties are used and shouldn't be. They are deprecated, because they leak the function caller, are non-standard, hard to optimize and potentially a performance-harmful feature.

示例

¥Examples

已弃用的 function.caller 或 arguments.callee

¥Deprecated function.caller or arguments.callee

Function.prototype.callerarguments.callee 已弃用(有关更多信息,请参阅参考文章)。

¥Function.prototype.caller and arguments.callee are deprecated (see the reference articles for more information).

js
"use strict";

function myFunc() {
  if (myFunc.caller === null) {
    return "The function was called from the top!";
  } else {
    return `This function's caller was ${myFunc.caller}`;
  }
}

myFunc();
// TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

Function.prototype.arguments

Function.prototype.arguments 已弃用(有关更多信息,请参阅参考文章)。

¥Function.prototype.arguments is deprecated (see the reference article for more information).

js
"use strict";

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}`);
// TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

也可以看看