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

rules.jshint.W031.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 'new' for side effects" error is thrown when JSLint, JSHint or ESLint encounters a function invocation preceded by the new operator when not part of an assignment or comparison expression. JSHint will only issue this warning if the nonew option is set to true. In the following example we call the built-in Date function as a constructor but don't assign the returned instance to anything:

x
 
1
/*jshint nonew: true */
2
new Date();
3
JSLint found 1 errorVersion 2015-09-23
Line 1:Unexpected expression 'new' in statement position.

Why do I get this error?

This error is raised to highlight a a lack of convention. While the code is perfectly valid it contravenes best practice, and in the case of the example above it indicates completely pointless code.

By not assigning the return value of a constructor to something you will lose the reference to that instance. Generally, by constructing an instance you would want to keep that reference, whether to use again later or for "internal" use as part of a comparison. What's the point of constructing something you are going to throw away as soon as it's been created?

If you have a constructor function that performs work beyond simply setting up an instance, and you are calling that constructor just for these "side effects", consider reworking your code to allow you to call the function normally, without the new operator. In the following simple example the side effect of calling the constructor is the incrementation of a variable:

12
 
1
/*jshint nonew: true */
2
var counter = 0;
3
4
function Person(name) {
5
    "use strict";
6
    this.name = name;
7
    counter += 1;
8
}
9
10
var me = new Person("James");
11
new Person(); // Increments 'counter' as a side-effect
12
JSLint found 2 errorsVersion 2015-09-23
Line 5:Unexpected 'this'.
Line 10:Unexpected expression 'new' in statement position.

In the above example we create two instances of Person but only keep the reference to one. The second call is simply there for the side effect of incrementing the counter. This example could be reworked to increment the counter without calling the constructor:

12
 
1
/*jshint nonew: true */
2
var counter = 0;
3
4
function Person(name) {
5
    "use strict";
6
    this.name = name;
7
    counter += 1;
8
}
9
10
var me = new Person("James");
11
counter += 1; // Don't use the constructor
12
JSLint found 1 errorVersion 2015-09-23
Line 5:Unexpected 'this'.

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

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





© 2015 - 2025 Weber Informatics LLC | Privacy Policy