Skip to content Skip to sidebar Skip to footer

Why Need To Use Semicolon Before Defining A Function?

I've seen some strange ; at the beginning of a function in some jQuery plugins source code like this: ;(function ($) {..... Can someone explain why they need to use ; in this case

Solution 1:

This semicolon will help you to properly concatenate a new code into a file when the current existed code in this file does not include a ; at the end.

For example:

(function() {

})()  // <--- No semicolon//  Added semicolon to prevent unexpected laziness result from previous code    
;(function ($) {

})();

Without the semicolon, the second () would have been interpreted as a function call, and will tried to call the return result of the first function

Solution 2:

This is just to make sure to terminate any previous instruction.

the semi colon before function invocation is a safety net against concatenated scripts and/or other plugins which may not be closed properly.

https://github.com/shichuan/javascript-patterns/blob/master/jquery-plugin-patterns/extend.html

Post a Comment for "Why Need To Use Semicolon Before Defining A Function?"