Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 234
travis Avatar asked Mar 04 '10 17:03

travis


People also ask

What happens when we use new keyword in JavaScript?

When a function is called with the new keyword, the function will be used as a constructor. new will do the following things: Creates a blank, plain JavaScript object. For convenience, let's call it newInstance .

What does the new keyword do?

New keyword in JavaScript is used to create an instance of an object that has a constructor function. On calling the constructor function with 'new' operator, the following actions are taken: A new empty object is created.

What is the word new in JavaScript?

Summary: The new keyword is used in javascript to create a object from a constructor function. The new keyword has to be placed before the constructor function call and will do the following things: Creates a new object. Sets the prototype of this object to the constructor function's prototype property.

What are side effects in JavaScript?

What is a side effect in JavaScript? When we modify something, in JavaScript, we cause side effects, this simply means modifying or changing our code, causing it to have unpredictable behavior and mutability.


2 Answers

JsLint itself gives you the reason:

Constructors are functions that are designed to be used with the new prefix. The new prefix creates a new object based on the function's prototype, and binds that object to the function's implied this parameter. If you neglect to use the new prefix, no new object will be made and this will be bound to the global object. This is a serious mistake.

JSLint enforces the convention that constructor functions be given names with initial uppercase. JSLint does not expect to see a function invocation with an initial uppercase name unless it has the new prefix. JSLint does not expect to see the new prefix used with functions whose names do not start with initial uppercase. This can be controlled with the newcap option.

JSLint does not expect to see the wrapper forms new Number, new String, new Boolean.

JSLint does not expect to see new Object (use {} instead).

JSLint does not expect to see new Array (use [] instead).

like image 74
Elzo Valugi Avatar answered Nov 15 '22 17:11

Elzo Valugi


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:

  1. New object is created (via new)
  2. 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.

like image 40
Doug Neiner Avatar answered Nov 15 '22 19:11

Doug Neiner