問(wèn)題描述
對(duì)于函數(shù)(function($){})(),我看到里面有jQuery這個(gè)詞,這是為什么呢?
For the function (function($){})(), I've seen it with the word jQuery in it, why is that?
我已閱讀此頁(yè)面 Javascript:為什么 jQuery 會(huì)這樣做:(function(){ ...});,它是如何工作的? 但它沒(méi)有說(shuō)明這樣的示例:
I have read this page Javascript: why does jQuery do this: (function(){ ...});, and how does it work? but it didnt tell about an example like this:
(function($){})(jQuery);
括號(hào)中的那個(gè)jquery是做什么的?如果括號(hào)本身已經(jīng)調(diào)用了函數(shù),那我們?yōu)槭裁葱枰莻€(gè) jQuery 呢?
What does that jquery in the parenthesis do? If the parenthesis by itself already self calls the function then why do we need that jQuery in there?
推薦答案
是一個(gè)簡(jiǎn)單的函數(shù)調(diào)用,jQuery對(duì)象作為參數(shù)發(fā)送給匿名函數(shù),例如:
Is a simple function invocation, the jQuery object is sent as an argument to the anonymous function, e.g.:
(function (foo) {
alert(foo); // alerts "hello"
})("hello");
這是定義插件的常用模式,基本上允許您在匿名函數(shù)范圍內(nèi)將jQuery對(duì)象引用為$
,即使jQuery運(yùn)行在noConflict
模式.
It's a common pattern to define plugins, basically permits you to reference the jQuery object as $
in the scope of the anonymous function, even if jQuery is running in noConflict
mode.
這篇關(guān)于對(duì)于函數(shù)(function($){})(),我看到里面有jQuery這個(gè)詞,這是為什么呢?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!