Bad constructor

When do I get this error?

The "Bad constructor" error is thrown when JSLint or JSHint encounters the new operator followed by a literal value. In the following example we are attempting to apply the new operator to a numeric literal:

var num = new 5();

Why do I get this error?

In the case of assignment to a function call this error is raised to highlight a fatal type error. Your code will throw an error in all environments if you do not resolve this issue.

The new operator attempts to invoke the internal [[Construct]] property of its operand (ES5 §11.2.2):

The production NewExpression : new NewExpression is evaluated as follows:

    1. Let ref be the result of evaluating NewExpression.
    2. Let constructor be GetValue(ref).
    3. If Type(constructor) is not Object, throw a TypeError exception.
    4. If constructor does not implement the [[Construct]] internal method, throw a TypeError exception.

We are particularly interested in steps 3 and 4. In the example above the operand is a numeric literal and therefore its type is not "Object". As stated by the third rule this will cause a type error to be thrown.

If the type of the operand is Object but it does not have an internal [[Construct]] method the same thing happens and a type error is thrown in step

  1. JSLint and JSHint can detect this to a point and will issue the same "Bad constructor" warning if you attempt to apply new to an object literal:
var num = new {}();

To avoid this warning simply stop attempting to misuse the new operator. It is only useful for creating instances of a constructor function and has no sensible meaning when applied to non-function objects or literals.

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 W056. This means you can tell JSHint to not issue this warning with the /*jshint -W056 */ directive.

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.