Why use a semicolon before defining a function?

I saw a few strange ones ; at the beginning of the function in some jQuery plugin source code:

 ;(function ($) {..... 

Can someone explain why you should use in this case ; ?

+7
source share
2 answers

This semicolon will help you correctly merge the new code into a file if the current existing code in this file does not contain ; in the end.

For example:

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

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

+11
source

This is just to make sure that terminate any previous statement.

The comma before the function call is a protective network against concatenated scripts and / or other plugins that cannot be closed properly.

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

+5
source

All Articles