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.
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 .
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.
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 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.
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).
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With