問題描述
對于函數(function($){})(),我看到里面有jQuery這個詞,這是為什么呢?
For the function (function($){})(), I've seen it with the word jQuery in it, why is that?
我已閱讀此頁面 Javascript:為什么 jQuery 會這樣做:(function(){ ...});,它是如何工作的? 但它沒有說明這樣的示例:
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);
括號中的那個jquery是做什么的?如果括號本身已經調用了函數,那我們?yōu)槭裁葱枰莻€ 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?
推薦答案
是一個簡單的函數調用,jQuery對象作為參數發(fā)送給匿名函數,例如:
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");
這是定義插件的常用模式,基本上允許您在匿名函數范圍內將jQuery對象引用為$
,即使jQuery運行在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.
這篇關于對于函數(function($){})(),我看到里面有jQuery這個詞,這是為什么呢?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!