久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

JavaScript 中的關(guān)鍵字“new"有什么副作用?

What side effects does the keyword #39;new#39; have in JavaScript?(JavaScript 中的關(guān)鍵字“new有什么副作用?)
本文介紹了JavaScript 中的關(guān)鍵字“new"有什么副作用?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在為 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:

  1. 創(chuàng)建新對(duì)象(通過 new)
  2. 使用 jQuery 的 data 將實(shí)例附加到 DOM 元素:$(el).data('FasterTrim', this)
  1. New object is created (via new)
  2. 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創(chuàng)建頭像的 jQuery/JavaScript 庫?)
How to do following mask input problem?(如何做以下掩碼輸入問題?)
Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 設(shè)置值/標(biāo)簽的問題)
how to unit-test private methods in jquery plugins?(如何對(duì) jquery 插件中的私有方法進(jìn)行單元測試?)
stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 為垂直滾動(dòng)網(wǎng)站配置偏移量/對(duì)齊元素?)
jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽輸入插件.當(dāng)文本框獲得焦點(diǎn)時(shí)選擇所有內(nèi)容)
主站蜘蛛池模板: 99精品免费在线观看 | 99精品网 | 亚洲精品区| 欧美xxxx色视频在线观看免费 | 欧美成年人视频在线观看 | 日本精品视频一区二区三区四区 | 日韩欧美在线精品 | 欧美一级小视频 | 亚洲第一区国产精品 | 国产激情在线看 | 久久出精品 | 在线播放一区二区三区 | 国产高清精品在线 | 妹子干综合 | 激情av网站 | 国产清纯白嫩初高生视频在线观看 | 99精品一区二区 | 国产偷久久一级精品60部 | 欧日韩在线 | 亚洲先锋影音 | 亚洲国产欧美在线 | 天天搞夜夜操 | 久久中文免费视频 | www.国产一区 | www.国产日本 | 一区二区三区亚洲视频 | 精品日韩一区二区 | www.久久久.com | 欧美三区| 中文字幕在线精品 | 麻豆久久久久久久 | 婷婷丁香在线视频 | 国产精品夜色一区二区三区 | 狠狠综合久久av一区二区老牛 | 欧洲精品在线观看 | 国产精品福利久久久 | 中国xxxx性xxxx产国 | 国产福利视频在线观看 | 亚州一区二区三区 | 亚洲欧美久久 | 国产成人在线播放 |