Javscript Linter – Why do you hate new?

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?

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>