0

I found this snippet when i was looking at jQuery plugins and wonder what it actually does

A jQuery plugin skeleton:

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

And more recently in nettuts:

var STICKIES = (function () {
    ...
}()); 
Andy E
  • 338,112
  • 86
  • 474
  • 445
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • possible duplicate of [What does this JavaScript/jQuery syntax mean?](http://stackoverflow.com/questions/2309614/what-does-this-javascript-jquery-syntax-mean) – James Wiseman Aug 11 '10 at 12:48

3 Answers3

7

This creates a anonymous function and calls it directly: this is equivalent to

var fun = function(){};
fun();

its used in jquery plugins to ensure compatibility with other libraries defining a global variable '$'. in your plugin sekeleton, you wrap your plugin in a anonymous function, which receives an argument named '$' (thus overriding a global variable '$'), this anonymous function is then called with 'jQuery' as parameter, so effectively $ becomes = jQuery, but only within that anonymous function.

Andy E
  • 338,112
  • 86
  • 474
  • 445
Cice
  • 151
  • 5
0

The first part:

function($) {
    ... 
}

creates an anonymous function. The second part: wrapping this function with braces and (jQuery); cal the function with jQuery as argument (usable via $ in the function).

nettuts then saves the result of the call in the variable.

dst
  • 1,770
  • 13
  • 26
0

The first function means that $ is overwritten by jQuery, which is useful if you have given a different meaning to '$' in the script.

AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120