I’m trying backbone and playing with the model concept – something I’ve implemented without a framework in the past. I’m also trying sublime text with the javascript linter turned on, and have noticed it hates “new” quite a bit.
var StandardMethod = Backbone.Model.extend({
initialize : function(){
console.log('init');
}
});
var LintOk = Backbone.Model.extend((function(){
this.initialize = function(){
console.log('init');
};
return this;
})());
var LintHates = Backbone.Model.extend(new function(){
this.initialize = function(){
console.log('init');
};
});
var sm = new StandardMethod();
var lo = new LintOk();
var lh = new LintHates();
The LintOk method requires changes in 3 different places in order to create a closure-compatible function. So if I want some closure variables like:
var NowWithClosures = Backbone.Model.extend(new function(){
var x = 1;
this.initialize = function(){
console.log('init');
};
this.AddOneToX = function(){
x++;
};
this.getX = function() {
return x;
};
});
var nwc = new NowWithClosures();
nwc.AddOneToX();
console.log(nwc.getX());
I have to use this very verbose method in order to be lint-approved? Is there something I’m missing here? What’s the reasoning for this? I could “return this;” in all my model definitions but that seems silly, and not intuitive – the model definition might be more than one screen long and the “new” call would be at the top making it more obviously an anonymous constructor.
Can be replaced with:
Passes JSLint: