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();
何时不使用
¥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 中引入。