admin管理员组文章数量:1320593
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.
本文标签:
版权声明:本文标题:javascript - JSLint errors on 'new function(){...}' but why? If I want anonymous closures, is there a better met 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742071207a2419134.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论