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

前端 未结 6 2070
忘了有多久
忘了有多久 2021-02-07 01:58

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.faste         


        
相关标签:
6条回答
  • 2021-02-07 02:32

    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.

    0 讨论(0)
  • 2021-02-07 02:35

    Basically JavaScript tends to be a slow beast, so creating a new object just to call a function is quite inefficient. The function is static anyway.

    $.fasterTrim(this, options);
    
    0 讨论(0)
  • 2021-02-07 02:40

    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).

    0 讨论(0)
  • 2021-02-07 02:42

    It's complaining because you're calling "new" but then throwing away the returned object, I bet. Why is that code using "new"? In other words, why isn't it just

    jQuery.fasterTrim(this, options);
    

    edit OK, well that "Starter" tool generates the code that way because it really does want a new object created, and yes it really is to take advantage of side effects. The constructor code that "Starter" generates stashes a reference to the new object on the affected element, using the jQuery "data" facility.

    0 讨论(0)
  • 2021-02-07 02:50

    You are using new to perform some action instead of to create an object and return it. JSLint considers this an invalid use of new.

    You should either use it like this:

    var x = new SomeConstructor();
    

    Or perform some action like this:

    SomeMethod();
    

    But never use new to perform an action like this:

    new SomeCosntructor(args);
    

    Doing so is considered using new for side effects because you aren't using it to create an object.

    0 讨论(0)
  • 2021-02-07 02:54

    From jQuery fasterTrim source code:

     * Usage: 
     * 
     * $(element).fasterTrim(options);  // returns jQuery object
     * $.fasterTrim.trim(" string ", options);  // returns trimmed string
    

    To answer the question, "Do not use new for side effects" means:

    Do not use new for what the constructor will do to its parameters but to create an object, side effects in constructors are baaaad!

    0 讨论(0)
提交回复
热议问题