Skip to content
← Back to rules

eslint/no-caller 正确性

This rule is turned on by default.

它做了什么

禁止使用 arguments.callerarguments.callee

为什么这是不好的?

使用 arguments.callerarguments.callee 会使得多种代码优化无法进行。它们在 JavaScript 中已被弃用,在严格模式下使用这些属性是被禁止的。

js
function foo() {
  var callee = arguments.callee;
}

此规则旨在通过禁止使用 arguments.callerarguments.callee 来 discouraging(阻止)使用已弃用且次优的代码。因此,当使用 arguments.callerarguments.callee 时,该规则会发出警告。

有关更多信息,请参阅 MDN 文档

示例

此规则的 错误 代码示例:

js
function foo(n) {
  if (n <= 0) {
    return;
  }

  arguments.callee(n - 1);
}

[1, 2, 3, 4, 5].map(function (n) {
  return !(n > 1) ? 1 : arguments.callee(n - 1) * n;
});

此规则的 正确 代码示例:

js
function foo(n) {
  if (n <= 0) {
    return;
  }

  foo(n - 1);
}

[1, 2, 3, 4, 5].map(function factorial(n) {
  return !(n > 1) ? 1 : factorial(n - 1) * n;
});

如何使用

要通过配置文件或 CLI 启用 此规则,可以使用:

json
{
  "rules": {
    "no-caller": "error"
  }
}
bash
oxlint --deny no-caller

参考资料