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-patible 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.
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-patible 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.
Share Improve this question asked Jun 8, 2012 at 18:03 Will ShaverWill Shaver 13.1k6 gold badges51 silver badges65 bronze badges 6-
1
Why would you want to do this as every model you instantiate would share the same
x
. Also, in yourLintOK
snippet,this
will be the global object. – Esailija Commented Jun 8, 2012 at 18:07 - There might be cases when I want the models to share the same x. – Will Shaver Commented Jun 8, 2012 at 18:15
-
Excellent point about
this
being the global object in theLintOk
version. The global object is obviously not what I want here! – Will Shaver Commented Jun 8, 2012 at 18:19 -
3
Yes there's a better method: Use jsHint. instead. It's very configurable, and has a setting to allow that. See
supernew
on the options page. – user1106925 Commented Jun 8, 2012 at 18:42 - 1 That 'new' keyword is sure 'super'! Thanks @am-not-i-am – Will Shaver Commented Jun 8, 2012 at 18:48
4 Answers
Reset to default 4Backbone.Model.extend(new function(){
var x;
this.value = "";
this.func = function() {
return x;
};
});
Can be replaced with:
Backbone.Model.extend(function(){
var x;
return {
value: "",
func: function(){
return x;
}
};
}())
Passes JSLint:
var Backbone;
Backbone.Model.extend((function () {
"use strict";
var x;
return {
value: "",
func: function () {
return x;
}
};
}()));
JSLint does not like doing new anonFn
.
You can just do:
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 backbonefn = function() {
this.initialize = function() {
console.log('init');
}
};
var LintHates = Backbone.Model.extend(new backbonefn);
var sm = new StandardMethod();
var lo = new LintOk();
var lh = new LintHates();
Click on JSLint in this fiddle: http://jsfiddle/maniator/Ztycf/
It should work just fine, but crockford insists that you move the call
of the self-invoking anonymous function inside the brackets were the function is defined.
For example, when i validate this code with jslint :
(function () { })();
it gives me this error : Move the invocation into the parens that contain the function.
After I modify the code like this :
(function () { }());
it doesn't shout errors any more.
p.s. it really doesn't quite matter because the result is the same (of course I don't know about ie6<), but I always use the first method for self-invoking functions.
If you actually run that code through jslint, the error mentioned is Weird construction. Delete 'new'
. You don't need to specify new function
when making anonymous functions in javascript.