
rules.jshint.W031.html Maven / Gradle / Ivy
Show all versions of sonar-web-frontend-js Show documentation
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 */
2new Date();
3
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 */
2var counter = 0;
3
4function Person(name) {
5 "use strict";
6 this.name = name;
7 counter += 1;
8}
9
10var me = new Person("James");
11new Person(); // Increments 'counter' as a side-effect
12
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 */
2var counter = 0;
3
4function Person(name) {
5 "use strict";
6 this.name = name;
7 counter += 1;
8}
9
10var me = new Person("James");
11counter += 1; // Don't use the constructor
12
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
.