admin管理员组文章数量:1356245
I have a self-contained Backbone.View
implementation called MainControllerView
that can handle itself (i.e., no reason to have an outside reference to it.). If, in my main bootstrapper function I kick things off like this:
$(function() {
new MainControllerView();
});
JSLint/JSHint plain that I am using "new for side effects." Reading up on this warning indicates that the above is considered smelly code. The alternatives are to not use new at all and just invoke the constructor as a function, or to assign it to a variable. However, calling my MainControllerView()
directly as a function without using new
raises errors in the backbone code, so that's apparently not an option. And its seems totally wrong to me that the following is somehow better code:
$(function() {
var instantGarbage = new MainControllerView();
});
In fact this raises a different JSLint warning "instantGarbage is defined but never used." Apparently, I'm danged if I do and I'm danged if I don't. So, is there a different "right" way to handle this sort of thing? Is creating instant garbage variables somehow the "better code" alternative? Is Backbone.js leveraging the "new" keyword in a non-Crockford-approved way? Or is this just one of the exceptions to the "rule"?
I have a self-contained Backbone.View
implementation called MainControllerView
that can handle itself (i.e., no reason to have an outside reference to it.). If, in my main bootstrapper function I kick things off like this:
$(function() {
new MainControllerView();
});
JSLint/JSHint plain that I am using "new for side effects." Reading up on this warning indicates that the above is considered smelly code. The alternatives are to not use new at all and just invoke the constructor as a function, or to assign it to a variable. However, calling my MainControllerView()
directly as a function without using new
raises errors in the backbone code, so that's apparently not an option. And its seems totally wrong to me that the following is somehow better code:
$(function() {
var instantGarbage = new MainControllerView();
});
In fact this raises a different JSLint warning "instantGarbage is defined but never used." Apparently, I'm danged if I do and I'm danged if I don't. So, is there a different "right" way to handle this sort of thing? Is creating instant garbage variables somehow the "better code" alternative? Is Backbone.js leveraging the "new" keyword in a non-Crockford-approved way? Or is this just one of the exceptions to the "rule"?
Share Improve this question edited Jan 16, 2014 at 20:01 B Robster asked Jan 16, 2014 at 19:23 B RobsterB Robster 42.1k24 gold badges92 silver badges124 bronze badges 2- possible duplicate of Is JavaScript 's "new" Keyword Considered Harmful? – tkone Commented Jan 16, 2014 at 19:37
- Not really a duplicate. I'm not asking whether new is harmful in and of itself, I'm asking whether its a problem to use it without assigning it to a variable. – B Robster Commented Jan 16, 2014 at 20:04
3 Answers
Reset to default 10new
is not harmful. Repeat over and over again.
If used correctly, new
works very well. Like in Backbone.
JSLint is a very opinionated linter; just because Crockford says something is bad or shouldn't be used, doesn't mean that's a universal truth. Plus, his reasoning behind it is more on the end that it obscures Javascript prototypal nature behind a more classical-oo façade.
However, if you use a tool like JSHint, you can configure these warnings.
I prefer the latter of your two declarations
$(function() {
var instantGarbage = new MainControllerView();
});
You can suppress this with the unused: false
option to JSHint.
Or you can set nonew: false
instead.
These can be set in a .jshintrc
or even per-file with a ment:
/* jshint unused: false */
At the top of your file. This will turn it off for the file. You can even disable it for specific scopes.
Check out the jshint documentation
Ok first of all Crockford is a human being with opinions; nothing more, nothing less. A great number of people disagree with many of his opinions, which is why the JSHint library is far more popular than JSLint even though JSLint had the advantage of ing first.
I'd strongly remend switching to JSHint so that you can stop worrying about silly things that Crockford cares about that don't actually make your code worse.
Look at it from someone else reading your code for the first time. Constructors are generally used to create a new object, which you then act upon. Looking at this, it appears that you just make a new View, and immediately throw it away 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./blargh/2014/4/constructors-should-not-have-side-effects
本文标签: javascriptDoes Backbonejs quotuse new for side effectsquot contrary to JSHintStack Overflow
版权声明:本文标题:javascript - Does Backbone.js "use new for side effects" contrary to JSHint? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743948918a2566922.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论