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 complain 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"?