1

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?

Community
  • 1
  • 1
dramasea
  • 3,370
  • 16
  • 49
  • 77

2 Answers2

0

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.

Oswald
  • 31,254
  • 3
  • 43
  • 68
0

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.

parent5446
  • 898
  • 7
  • 17