开发者

what is( function($){.....})(jQuery) that often used in the making of jquery plugin [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

a simple question on jquery closure

I often 开发者_JAVA百科see the code of jquery plugin consist this code, Can I know what it is? and what the purpose for?


Knowing little about jQuery, but something about JavaScript, I'd guess this declares an anonymous function that receives one argument (accessible using $ in the body of the function) and immediately calls this function, passing the jQuery object as argument.


How jQuery plugins usually work is that they make a function that adds the plugin to jQuery, and then immediately call that function so the plugin is installed. The notation

(function($) { ..code... })(jQuery)

makes a nameless function and then calls that function, passing the jQuery object as an argument. And then somewhere in the code of that function, you should find a

$.fn.newPlugin = ...

This adds the plugin into jQuery. This is done so that no conflicts are created; using this notation, no function names or variable names are actually declared.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜