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

JavaScript 中的關鍵字“new"有什么副作用?

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

問題描述

我正在為 jQuery 開發一個插件,我收到了這個 JSLint 錯誤:

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

我沒有太多運氣找到有關此 JSLint 錯誤或 new 可能具有的任何副作用的信息.

I haven't had much luck finding info on this JSLint error or on any side effects that new might have.

我試過 谷歌搜索不要使用'新'來獲得副作用."并得到 0 個結果.必應給了我 2 個結果,但它們都只是引用了 JSLint 源.希望這個問題會改變這一點.:-)

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 網站的開發者.

Travis, I am the developer behind the Starter site.

@Pointy 一針見血.以這種方式編寫 Starter 代碼的原因是因為我們確實需要一個新對象,我們只是不需要在那時存儲對它的引用.

@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);

會像你發現的那樣安撫 JSLint.

will appease JSLint as you have found.

Starter 插件設置遵循 jQuery UI 模式,即在元素的 data 集中存儲對對象的引用.這就是正在發生的事情:

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)
  2. 使用 jQuery 的 data 將實例附加到 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)

返回的對象沒有用處,因此沒有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.

更多背景知識:

使用 data 存儲對象的好處是我們可以在以后隨時通過調用訪問該對象:$("#your_selector").data('FasterTrim').但是,如果您的插件不需要以這種方式在中間流中訪問(意思是,它在一次調用中設置并且不提供未來交互),則不需要存儲引用.

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.

這篇關于JavaScript 中的關鍵字“new"有什么副作用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創建頭像的 jQuery/JavaScript 庫?)
How to do following mask input problem?(如何做以下掩碼輸入問題?)
Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 設置值/標簽的問題)
how to unit-test private methods in jquery plugins?(如何對 jquery 插件中的私有方法進行單元測試?)
stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 為垂直滾動網站配置偏移量/對齊元素?)
jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽輸入插件.當文本框獲得焦點時選擇所有內容)
主站蜘蛛池模板: 日韩成人在线观看 | 欧美大片在线观看 | 久久久xx | 欧美精品乱码久久久久久按摩 | 男人影音 | 日本三级日产三级国产三级 | 午夜精品一区 | 国产精品91久久久久久 | 成人国产免费观看 | 国产精品成人免费 | 天天久久 | 国产国产精品久久久久 | 热久久免费视频 | 中文字幕在线一区二区三区 | 91精品国产一区二区三区 | 狠狠影院 | 中文字幕一区二区三区不卡 | 国产精品高潮呻吟久久av野狼 | 丁香综合 | 在线国产一区二区三区 | 伊人狠狠 | 久久中文字幕av | 欧美在线视频一区二区 | 欧美日韩一二三区 | 午夜在线影院 | 龙珠z在线观看 | 一区二区av | 亚洲国产精品自拍 | 精品国产乱码久久久久久闺蜜 | 国产a级黄色录像 | 99爱在线观看 | 日批日韩在线观看 | 三级黄色网址 | 亚洲欧美国产毛片在线 | 一区精品国产欧美在线 | 天天干天天谢 | 综合久久99 | 熟女毛片 | 一区二区三区四区在线视频 | 精品av久久久久电影 | 国产一区二区免费 |