Missing '()' invoking a constructor

History

This warning has existed in two forms across the three main linters. It was introduced in the original version of JSLint and has remained in all three tools ever since.

  • In JSLint the warning given is the generic "Missing '{a}'"

  • In JSHint and ESLint the message used is "Missing '()' invoking a constructor"

The situations that produce the warning have not changed despite changes to the text of the warning itself.

When do I get this error?

The "Missing '()' invoking a constructor" error is thrown when JSLint, JSHint and ESLint encounter a new expression that is not immediately followed by a pair of parentheses. In the following example we create an instance of the built-in Date constructor:

var d = new Date;

Why do I get this error?

This error is raised to highlight a lack of convention. Your code will work without error if you do not resolve this issue but you may be contravening coding styles and best practices. The ECMAScript 5 specification shows (in a confusing way) that new expressions do not have to be followed by a pair of parentheses. The parentheses are only required when arguments are being passed to the constructor (ES5 §11.2):

MemberExpression :
    ...
    new MemberExpression Arguments

NewExpression :
    MemberExpression
    new NewExpression

Arguments :
    ( )
    ( ArgumentList )

The grammar can be a bit confusing, but in essence the above shows that the Arguments nonterminal is optional. If you do not need to pass arguments to the constructor you can leave it out. However, many style guides would recommend that the parentheses are always included for consistency, and to make it immediately clear that an invocation is taking place.

Consider the fact that omitting the parentheses from a normal (non-constructor) function invocation will cause the expression to evaluate to a reference to that function, rather than the return value of it. By missing the parentheses on a constructor call your code may be less self-explanatory. To fix the issue you can simply add the missing parentheses:

var d = new Date();

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W058. This means you can tell JSHint to not issue this warning with the /*jshint -W058 */ directive.

In ESLint the rule that generates this warning is named new-parens. You can disable it by setting it to 0, or enable it by setting it to 1.


About the author

James Allardice

This article was written by James Allardice, Software engineer at Tesco and orangejellyfish in London. Passionate about React, Node and writing clean and maintainable JavaScript. Uses linters (currently ESLint) every day to help achieve this.