对于函数(function($){})(),我在其中看到了jQuery这个词,为什么会这样? [英] For the function (function($){})(), I've seen it with the word jQuery in it, why is that?
问题描述
对于函数(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");
这是定义插件的常见模式,基本上允许你以<$ c $的形式引用jQuery对象c> $ 在匿名函数的范围内,即使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这个词,为什么会这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!