問題描述
我正在為 jQuery 開發(fā)一個(gè)插件,我收到了這個(gè) JSLint 錯(cuò)誤:
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));
我沒有太多運(yùn)氣找到有關(guān)此 JSLint 錯(cuò)誤或 new
可能具有的任何副作用的信息.
I haven't had much luck finding info on this JSLint error or on any side effects that new
might have.
我試過 谷歌搜索不要使用'新'來獲得副作用."并得到 0 個(gè)結(jié)果.必應(yīng)給了我 2 個(gè)結(jié)果,但它們都只是引用了 JSLint 源.希望這個(gè)問題會(huì)改變這一點(diǎn).:-)
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. :-)
更新 #1:以下是上下文的更多來源:
Update #1: Here's more source for the context:
jQuery.fn.fasterTrim = function(options) {
return this.each(function() {
(new jQuery.fasterTrim(this, options));
});
};
更新 #2:我使用 Starter jQuery 插件生成器 作為我的插件的模板,其中包含該代碼.
Update #2: I used the Starter jQuery plug-in generator as a template for my plug-in, which has that code in it.
推薦答案
Travis,我是 Starter
網(wǎng)站的開發(fā)者.
Travis, I am the developer behind the Starter
site.
@Pointy 一針見血.以這種方式編寫 Starter 代碼的原因是因?yàn)槲覀兇_實(shí)需要一個(gè)新對(duì)象,我們只是不需要在那時(shí)存儲(chǔ)對(duì)它的引用.
@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.
只需從
(new jQuery.fasterTrim(this, options));
到
var fT = new jQuery.fasterTrim(this, options);
會(huì)像你發(fā)現(xiàn)的那樣安撫 JSLint.
will appease JSLint as you have found.
Starter 插件設(shè)置遵循 jQuery UI 模式,即在元素的 data
集中存儲(chǔ)對(duì)對(duì)象的引用.這就是正在發(fā)生的事情:
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:
- 創(chuàng)建新對(duì)象(通過 new)
- 使用 jQuery 的
data
將實(shí)例附加到 DOM 元素:$(el).data('FasterTrim', this)
- New object is created (via new)
- The instance is attached to the DOM element using jQuery's
data
:$(el).data('FasterTrim', this)
返回的對(duì)象沒有用處,因此沒有var
聲明.我將考慮更改聲明并清理輸出以使 JSLint 開箱即用.
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.
更多背景知識(shí):
使用 data
存儲(chǔ)對(duì)象的好處是我們可以在以后隨時(shí)通過調(diào)用訪問該對(duì)象:$("#your_selector").data('FasterTrim')代碼>.但是,如果您的插件不需要以這種方式在中間流中訪問(意思是,它在一次調(diào)用中設(shè)置并且不提供未來交互),則不需要存儲(chǔ)引用.
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.
如果您需要更多信息,請(qǐng)告訴我.
Let me know if you need more info.
這篇關(guān)于JavaScript 中的關(guān)鍵字“new"有什么副作用?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!