require parentheses when invoking a constructor with no arguments (new-parens)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

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

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

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

/*eslint new-parens: "error"*/

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

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

/*eslint new-parens: "error"*/

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

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)();

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");

Version

This rule was introduced in ESLint 0.0.6.

Resources

Js中文网,专注分享前端最新技术、大厂面试题、聊点程序员轶事、职场感悟,做前端技术的传播者.

加入前端布道师交流群

扫描二维码回复 加群 学习,与大厂大佬讨论技术.

深入理解JavaScript系列