Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I address the JSLint warning "Do not use 'new' for side effects"?

Why do I get these errors?

Problem at line 329 character 60: Do not use 'new' for side effects.

new widget.StyledDropdown(dojo.byId("sTitle"));

Problem at line 330 character 61: Do not use 'new' for side effects.

new widget.StyledDropdown(dojo.byId("sSuffix"));

Problem at line 336 character 57: Do not use 'new' for side effects.

true,{shortenName : true,maxChars : 20});

Problem at line 338 character 129: Do not use 'new' for side effects.

new widget.StyledDropdown(dojo.byId("sCountry"),USPS.Address.countrySw...

like image 610
Amen Ra Avatar asked Sep 10 '10 16:09

Amen Ra


2 Answers

You're not storing a reference to the newly-created objects, which is a code smell.

JSLint is saying "You're creating some objects but immediately discarding them; the only possible reason you can be doing that is that the act of creating the objects has side-effects, which is weird."

You can lose the warning either by preventing your constructors having side effects (which will mean finding some other way of doing whatever it is they're doing, eg. by moving that code into a normal function) or by storing references to the newly-created objects (even in a temporary local variable that you discard).

like image 142
RichieHindle Avatar answered Oct 05 '22 23:10

RichieHindle


Rethinking the strategy is best, but more often, handling tech debt during a development cycle isn't an option.

If you are using JSHint you can override this option on a case by case basis. Add this jshint comment in the scope of the offending code.

/* jshint -W031 */
new widget.StyledDropdown(dojo.byId("sTitle"));
new widget.StyledDropdown(dojo.byId("sSuffix"));
...

Inline configurations are function scoped. So anything outside the scope of the comment will still be checked.

like image 45
Shanimal Avatar answered Oct 06 '22 01:10

Shanimal