Possible Duplicate:
a simple question on jquery closure
I often see the code of jquery plugin consist this code, Can I know what it is? and what the purpose for?
Possible Duplicate:
a simple question on jquery closure
I often 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.