What is the purpose of starting partial shade at the beginning of JavaScript?

Possible duplicate:
What does the leading semicolon do in JavaScript libraries?

I noticed that many jQuery plugins start with

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

I just wondered what the starting semicolon was for, as well as the empty parentheses at the end.

+84
javascript jquery
Aug 22 2018-11-11T00:
source share
2 answers

There is a semi-colon if you include this script immediately after some "bad" script that incorrectly closes its last line with a colon. In this case, it is possible that the two scripts will be combined and result in invalid code. For example, if you combine several scripts into one answer.

Function () at the end performs a function. This creates a closure. Private variables and methods can be declared as part of this function, which cannot be accessed from outside the script.

+119
Aug 22 2018-11-11T00:
source share

This design:

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

Used to create a new area in Javascript.

More details about the function area here.

As for the semicolon, I have never seen it before. I think this is safe when you combine multiple scripts, since in some cases semicolons are optional at the end of the file.

+9
Aug 22 2018-11-11T00:
source share



All Articles