问题
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?
回答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
回答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
来源:https://stackoverflow.com/questions/16319510/why-need-to-use-semicolon-before-defining-a-function