久久久久久久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 屏蔽輸入插件.當文本框獲得焦點時選擇所有內容)
主站蜘蛛池模板: 亚洲精品视频一区 | 韩国成人在线视频 | 色婷婷久久久亚洲一区二区三区 | 欧美成人一区二免费视频软件 | 男人的天堂视频网站 | 国产成人精品一区二区三 | 久久人人爽人人爽人人片av免费 | 国产精品久久久久久久岛一牛影视 | 日韩伦理一区二区三区 | 国产亚洲一区二区三区 | 欧美日韩高清在线一区 | 日韩久久久久久 | 久久亚洲一区 | 色婷婷av久久久久久久 | 亚洲综合久久精品 | 毛片免费看 | 午夜综合 | 中文字幕一区在线观看视频 | 亚洲精品国产区 | 精品一区二区久久久久久久网站 | 久草热播 | 91看国产 | 久在线 | 中文字幕日本一区二区 | 国产精品九九九 | 免费视频二区 | 97国产在线视频 | 亚洲精品自在在线观看 | 亚洲欧美国产视频 | 91精品久久久久久久久 | 久久噜噜噜精品国产亚洲综合 | av免费网站在线观看 | 国产精品国产精品国产专区不蜜 | 爱爱小视频 | 亚洲精品视频久久 | 国产一二区在线 | 超碰最新在线 | 女同av亚洲女人天堂 | 久久久99精品免费观看 | 欧美久久久网站 | 激情久久久久 |