gpt4 book ai didi

javascript - JavaScript 中关键字 'new' 有什么副作用?

转载 作者:行者123 更新时间:2023-12-03 21:45:58 25 4
gpt4 key购买 nike

我正在开发 jQuery 插件,我得到了这个 JSLint错误:

Problem at line 80 character 45: Do not use 'new' for side effects.

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

我没有找到有关此 JSLint 错误或 new 可能产生的任何副作用的信息。

我已经尝试过Googling “不要使用‘新’来产生副作用。”并得到 0 个结果。 Binging给了我 2 个结果,但它们都只引用了 JSLint 源代码。希望这个问题会改变这一点。 :-)

更新#1:以下是上下文的更多来源:

  jQuery.fn.fasterTrim = function(options) {
return this.each(function() {
(new jQuery.fasterTrim(this, options));
});
};

更新#2:我用了Starter jQuery plug-in generator作为我的插件的模板,其中包含该代码。

最佳答案

JsLint itself给你原因:

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

关于javascript - JavaScript 中关键字 'new' 有什么副作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2381253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com