Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
189
rated 0 times [  192] [ 3]  / answers: 1 / hits: 23919  / 13 Years ago, mon, august 22, 2011, 12:00:00

Possible Duplicate:

What does the leading semicolon in JavaScript libraries do?






I have noticed a lot of jQuery plugins start with



;(function(){ /* something in here */ })();


I just wondered what the beginning semi-colon was for, as well as the empty parentheses at the end.


More From » jquery

 Answers
531

The semi-colon is there in case you include this script just after some 'bad' script that doesn't properly close off its last line with a semi-colon. In this case it's possible the two scripts would be combined and result in invalid code. For example if you are merging multiple script into a single response.



The () at the end is executing the function. This is creating a closure. Private variables and methods can be declared within the scope of this function that cannot be accessed from outside the script.


[#90496] Friday, August 19, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alfredoc

Total Points: 261
Total Questions: 128
Total Answers: 89

Location: French Polynesia
Member since Sun, Aug 2, 2020
4 Years ago
;