Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

javascript - For the function (function($){})(), I've seen it with the word jQuery in it, why is that?

For the function (function($){})(), I've seen it with the word jQuery in it, why is that?

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);

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...