
rules.jshint.W056.html Maven / Gradle / Ivy
Show all versions of sonar-web-frontend-js Show documentation
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:
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
- 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 1var num = new {}();
2
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.