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

rules.jshint.W053.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 "Do not use {a} as a constructor" error is thrown when JSLint, JSHint or ESLint encounters a call to String, Number, Boolean, Math or JSON preceded by the new operator. In the following example we attempt to assign some values to variables by invoking these functions as constructors:

x
 
1
var str = new String("hello"),
2
    num = new Number(10),
3
    bool = new Boolean(false),
4
    math = new Math(),
5
    json = new JSON({ myProp: 10 });
6
JSLint found 5 errorsVersion 2015-09-23
Line 0:Unexpected 'new'.
Line 1:Unexpected 'new'.
Line 2:Unexpected 'new'.
Line 4:Unexpected space between '{' and 'myProp'.
Line 4:Unexpected space between '10' and '}'.

Why do I get this error?

This error is raised to highlight a bad practice and a piece of code that may not work as you intend it to. It can also highlight a possible fatal JavaScript error. Your code could run without error if you do not change it, but could be confusing to other developers and could also behave in unexpected ways.

The String, Number and Boolean constructor functions return objects of type String, Number and Boolean, which is rarely what you want. Usually, you want literal string, number or boolean values, because strictly comparing an object to a literal will always return false. In the case of these objects, to fix this error, use literal values rather than their corresponding wrapper objects:

4
 
1
var str = "hello",
2
    num = 10,
3
    bool = false;
4
JSLint found no errorsVersion 2015-09-23

Note that this does not cause you to lose any functionality, since literal values are internally cast to instances of the appropriate type when you call a method on them. Also note that you are free to use these functions to perform type conversions i.e. by invoking them without the new operator:

4
 
1
var str = String(10),    // "10"
2
    num = Number("123"), // 123
3
    bool = Boolean("");  // false
4
JSLint found no errorsVersion 2015-09-23

The case is a little different for the Math and JSON objects. These two objects are not functions, and cannot be constructed. Attempts to instantiate them will result in a type error. If you're trying to serialize an object into a JSON string, you need to use the JSON.stringify method instead:

2
 
1
var json = JSON.stringify({ myProp: 10 });
2
JSLint found 2 errorsVersion 2015-09-23
Line 0:Unexpected space between '{' and 'myProp'.
Line 0:Unexpected space between '10' and '}'.

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





© 2015 - 2025 Weber Informatics LLC | Privacy Policy