Index

new-parens

在调用不带参数的构造函数时强制或禁止使用括号

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

Important

This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.

Learn more

JavaScript 允许在通过 new 关键字调用函数时省略括号,前提是构造函数没有参数。然而,一些程序员认为省略括号与语言的其余部分不一致,因此会使代码不那么清晰。

🌐 JavaScript allows the omission of parentheses when invoking a function via the new keyword and the constructor has no arguments. However, some coders believe that omitting the parentheses is inconsistent with the rest of the language and thus makes code less clear.

var person = new Person;

规则详情

🌐 Rule Details

此规则可以在使用 new 关键字调用无参数构造函数时强制或禁止使用括号。

🌐 This rule can enforce or disallow parentheses when invoking a constructor with no arguments using the new keyword.

选项

🌐 Options

该规则采用一种选择。

🌐 This rule takes one option.

  • "always" 强制在没有参数(默认)的新构造函数后使用括号
  • "never" 强制在没有参数的新构造函数后不使用圆括号

always

使用 "always" 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the "always" option:

在线运行
/*eslint new-parens: "error"*/

var person = new Person;
var person = new (Person);

使用 "always" 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the "always" option:

在线运行
/*eslint new-parens: "error"*/

var person = new Person();
var person = new (Person)();

never

使用 "never" 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the "never" option:

在线运行
/*eslint new-parens: ["error", "never"]*/

var person = new Person();
var person = new (Person)();

使用 "never" 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the "never" option:

在线运行
/*eslint new-parens: ["error", "never"]*/

var person = new Person;
var person = (new Person);
var person = new Person("Name");

版本

此规则是在 ESLint v0.0.6 中引入。

资源