1

Which of these is correct - as a number of plugins I've used are showing me differences and I was wondering why?

(function ($) {
    //Code
})(jQuery);

(function ($) {
    //Code
}(jQuery));

;(function($) {
    //Code
}(jQuery));

I am assuming the first however wondering why I've seen the 2nd and 3rd iterations?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Andy
  • 18,723
  • 12
  • 46
  • 54

2 Answers2

5

They're all the same.

The initial ; makes it work even with buggy script combiners that don't add ;s between scripts.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • oh wow great :) thanks I was wondering the differences. So it doesn't really matter between the 1st and 2nd ? – Andy Oct 10 '11 at 16:18
  • Not at all, unless you add `new`. See http://stackoverflow.com/questions/5938802/are-function-and-function-functionally-equal-in – SLaks Oct 10 '11 at 16:20
1

FWIW, Ive always used the first when authoring jQuery plugins.

The second looks like it would also work, the third looks like the second while also ensuring that the previous line terminates in a semicolon... probably to aid in minification issues.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • yeah the same. However, I can't understand why the 2nd and 3rd iterations are around ? Unsure of the differences and why they are used? – Andy Oct 10 '11 at 16:17