Skip to content
← Back to rules

eslint/no-object-constructor Pedantic

🚧 An auto-fix is planned for this rule, but not implemented at this time.

它的作用

禁止在没有参数的情况下调用 Object 构造函数。

为什么这不好?

通常不建议使用 Object 构造函数来创建一个新的空对象,而应使用对象字面量表示法,因为它更简洁,而且全局的 Object 可能会被重新定义。例外情况是,当 Object 构造函数被用于有意包装一个作为参数传入的指定值时。

示例

此规则的错误代码示例:

js
Object();
new Object();

此规则的正确代码示例:

js
Object("foo");
const obj = { a: 1, b: 2 };
const isObject = (value) => value === Object(value);
const createObject = (Object) => new Object();

如何使用

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

json
{
  "rules": {
    "no-object-constructor": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则新增于 v0.13.2。

参考资料