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

前端 未结 6 2259
忘了有多久
忘了有多久 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: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).

提交回复
热议问题