new-parens
在调用不带参数的构造函数时强制或禁止使用括号
此规则报告的一些问题可通过 --fix
命令行选项自动修复
此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js
中使用 相应的规则。
¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js
.
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"
在不带参数的新构造函数之后强制使用括号(默认)¥
"always"
enforces parenthesis after a new constructor with no arguments (default) -
"never"
在不带参数的新构造函数之后强制不使用括号¥
"never"
enforces no parenthesis after a new constructor with no arguments
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 中引入。