no-object-constructor
禁止调用不带参数的 Object 构造函数
此规则报告的一些问题可通过编辑器 建议 手动修复
通常不鼓励使用 Object 构造函数来构造一个新的空对象,而更推荐使用对象字面量表示法,因为对象字面量更简洁,并且 Object 全局变量可能会被重新定义。唯一的例外是当使用 Object 构造函数来有意封装一个作为参数传入的指定值时。
🌐 Use of the Object constructor to construct a new empty object is generally discouraged in favor of object literal notation because of conciseness and because the Object global may be redefined.
The exception is when the Object constructor is used to intentionally wrap a specified value which is passed as an argument.
规则详情
🌐 Rule Details
此规则不允许在没有参数的情况下调用 Object 构造函数。
🌐 This rule disallows calling the Object constructor without an argument.
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint no-object-constructor: "error"*/
Object();
new Object();
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*eslint no-object-constructor: "error"*/
Object("foo");
const obj = { a: 1, b: 2 };
const isObject = value => value === Object(value);
const createObject = Object => new Object();
选项
🌐 Options
此规则没有选项。
🌐 This rule has no options.
何时不使用
🌐 When Not To Use It
如果你希望允许使用 Object 构造函数,则可以安全地关闭此规则。
🌐 If you wish to allow the use of the Object constructor, you can safely turn this rule off.
相关规则
版本
此规则是在 ESLint v8.50.0 中引入。