对于函数(function($){})(),我看到里面有jQuery这个词,这是为什么呢?
对于函数(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是做什么的?如果括号本身已经调用了函数,那我们为什么需要那个 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对象作为参数发送给匿名函数,例如:
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.
相关文章