Does Backbone.js use “new for side effects” contrary to JSHint?

I have a standalone Backbone.View implementation called MainControllerView that can handle itself (i.e. there is no reason to have an external link to it). If in my main boot function I start like this:

 $(function() { new MainControllerView(); }); 

JSLint / JSHint complain that I use "new for side effects." Reading this warning indicates that the above is considered a smelly code. Alternatives are not to use new ones at all and just call the constructor as a function or assign it to a variable. However, calling the MainControllerView() function directly as a function without using new causes errors in the base code, so this is apparently not an option. And it seems completely wrong to me that the following is somehow better code:

 $(function() { var instantGarbage = new MainControllerView(); }); 

This actually triggers another JSLint warning: "instantGarbage is defined but never used." Apparently, they will cancel me if I do this, and they will cancel me if I do not. So, is there another “right” way to deal with such things? Is creating instant garbage variables an alternative to "better code"? Is Backbone.js the keyword "new" is not endorsed by Crockford? Or is this just one of the exceptions to the "rule"?

+7
javascript jslint
source share
3 answers

new not harmful. Repeat again and again.

Used correctly, new works very well. Like in the backbone.

JSLint is a very stubborn linter; just because Crockford says that something is bad or should not be used does not mean that universal truth. In addition, his reasoning behind this is more at the end that he hides Javascript prototyping behind a more classic facade.

However, if you use a tool like JSHint, you can configure these warnings.

I prefer the last of your two ads

 $(function() { var instantGarbage = new MainControllerView(); }); 

You can suppress this with the unused: false option for JSHint.

Or you can set nonew: false instead.

They can be installed in .jshintrc or even in a comment file:

 /* jshint unused: false */ 

At the top of the file. This will disable the file. You can even disable it for certain areas.

See the jshint documentation

+9
source share

Well, first of all, Crockford is a person with opinions; no more, no less. Many people disagree with many of his opinions, so the JSHint library is much more popular than JSLint, although the advantage of JSLint was that it came first.

I highly recommend switching to JSHint so you can stop worrying about the stupid things that Crockford cares about that don't really make your code worse.

+1
source share

Look at this from someone else reading your code for the first time. Constructors are typically used to create a new object, which you then execute. Looking at it, it seems that you just create a new look and immediately drop it without doing anything.

So yes, with or without JS [L / H] int, this is a "bad code smell." Take a look at this article -

http://tmont.com/blargh/2014/4/constructors-should-not-have-side-effects

+1
source share

All Articles