What side effects does the keyword 'new' have in JavaScript?

javascript, jquery, jquery-plugins, jslint, new-operator

Solution

Travis, I am the developer behind the `Starter` site.

@Pointy hit the nail on the head. The reason the Starter code is written that way is because we do need a new object, we just don't need to store a reference to it at that point.

Simply changing the command from

(new jQuery.fasterTrim(this, options)); 

to

var fT = new jQuery.fasterTrim(this, options);

will appease JSLint as you have found.

The Starter plugin setup follows the jQuery UI pattern of storing a reference to the object in the `data` set for the element. So this is what is happening:

- New object is created (via new)

- The instance is attached to the DOM element using jQuery's `data` :`$(el).data('FasterTrim', this)`

There is no use for the object that is returned, and thus no `var` declaration made. I will look into changing the declaration and cleaning up the output to pass JSLint out of the box.

A little more background:

The benefit to storing the object using `data` is that we can access the object later at any time by calling: `$("#your_selector").data('FasterTrim')`. However, if your plugin does not need to be accessed mid stream that way (Meaning, it gets set up in a single call and offers no future interaction) then storing a reference is not needed.

Let me know if you need more info.

Problem

I'm working on a plug-in for jQuery and I'm getting this JSLint error: ``` Problem at line 80 character 45: Do not use 'new' for side effects. (new jQuery.fasterTrim(this, options)); ``` I haven't had much luck finding info on this JSLint error or on any side effects that `new` might have. I've tried Googling for "Do not use 'new' for side effects." and got 0 results. Binging gives me 2 results but they both just reference the JSLint source. Hopefully this question will change that. :-) Update #1: Here's more source for the context: ``` jQuery.fn.fasterTrim = function(options) { return this.each(function() { (new jQuery.fasterTrim(this, options)); }); }; ``` Update #2: I used the Starter jQuery plug-in generator as a template for my plug-in, which has that code in it.

Original source