All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rules.jshint.W056.html Maven / Gradle / Ivy

Go to download

Consume reports generated by jshint for code quality. Also consume reports for code duplication (either simian or cpd). Consumes the unit/integration tests reports (generated by Jasmin) coverage report (lcov generated by Istanbul). The information generated by reports are added in Sonar

There is a newer version: 2.1.0
Show newest version

    

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:

x
 
1
var num = new 5();
2
JSLint found 1 errorVersion 2015-09-23
Line 0:Unexpected '('.

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:
2
 
1
var num = new {}();
2
JSLint found 2 errorsVersion 2015-09-23
Line 0:Unexpected space between 'new' and '{'.
Line 0:Unexpected '('.

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.





© 2015 - 2025 Weber Informatics LLC | Privacy Policy