Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  21] [ 5]  / answers: 1 / hits: 36726  / 13 Years ago, fri, september 9, 2011, 12:00:00

What is the benefit of using semicolon before a self-invoking function in JavaScript? I saw this approach in few popular jQuery plugins and I'm curious to find if this is the next awesome thing in JavaScript that I don't know.


More From » jquery

 Answers
36

If you concatenate two files with self-invoking functions together that look like this:



File A:



(function(){...A...})()


File B:



(function(){...B...})()


File A+B:



(function(){...A...})()(function(){...B...})()


You have two statements without separator. This happens when you cat files together and then minify them.



Now the author of file B puts a semicolon in front:



File B2:



;(function(){...B2...})()


And you'll get a working script:



(function(){...A...})();(function(){...B2...})()

[#90178] Thursday, September 8, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ignacio

Total Points: 467
Total Questions: 128
Total Answers: 79

Location: Luxembourg
Member since Tue, Mar 14, 2023
1 Year ago
ignacio questions
;