Skip to content
← Back to rules

eslint/no-obj-calls 正确性

This rule is turned on by default.

它的作用

禁止将某些全局对象作为函数调用。

对于 TypeScript 代码,此规则可以禁用,因为 TypeScript 编译器会强制执行此检查。

为什么这不好?

某些全局对象并不应被当作函数调用。 将它们作为函数调用通常会抛出 TypeError。

示例

以下是此规则的错误代码示例:

javascript
let math = Math();
let newMath = new Math();

let json = JSON();
let newJson = new JSON();

let atomics = Atomics();
let newAtomics = new Atomics();

let intl = Intl();
let newIntl = new Intl();

let reflect = Reflect();
let newReflect = new Reflect();

以下是此规则的正确代码示例:

javascript
let area = (r) => 2 * Math.PI * r * r;
let object = JSON.parse("{}");
let first = Atomics.load(sharedArray, 0);
let segmenterFrom = Intl.Segmenter("fr", { granularity: "word" });

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "no-obj-calls": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-obj-calls": "error",
  },
});
bash
oxlint --deny no-obj-calls

版本

此规则在 v0.0.7 中添加。

参考资料