This article transferred from: http://blog.csdn.net/h_o_w_e/article/details/51388500
!function () {} (); ! What's the use?
From the syntax, the semicolon in JavaScript indicates the end of the statement, and at the beginning, it may be used for compression and other methods to split, indicating a new statement begins. So, if in a separate JS file, the beginning of the semicolon is meaningless, can be deleted.
An exclamation point is a logical operator, a "non", a common use of the IF (!true) {}, whereas the operator is preceded by the function definition as a whole, then the function is called, and the returned structure is logically calculated.
Plainly, here is the building of an immediate execution function, it is recommended that the landlord know the meaning can be, when they write, you can use a clearer format:
(function () {}) ();
The upstairs has been given a very detailed explanation.
Add a point here: Add "()" after the function Declaration "functions () {}", meaning that the function declaration is executed. Some are called "anonymous auto-execute functions" and more accurately represent "execute functions immediately".
But execute the function immediately, as part of the expression, or the JS interpreter will report a syntax error. In general, the easiest way to understand an expression is to add a parenthesis to the left and right ends, such as "(function () {})". Therefore, the most common notation for executing functions immediately is "(function () {})", which is a statement that can be added "before and after"; 】。
In fact, the immediate execution of the function is also a n-type, such as "(function () {})" is also OK, before the function declaration with a unary operator also line: "!function () {} ()", "~function () {} ()". Even a combination of unary operators can: "!!!! function () {} () "," ~~~function () {} () ","!~!function () {} () ").
The semicolon is to be separated from the preceding code, JS can be separated by line break code, but after merging and compressing multiple JS files, line breaks will generally be deleted, so even together may be wrong, plus a semicolon insurance.
The exclamation mark you see is usually in the compressed JS file, because in the anonymous function call, usually we are using: (function () {}) () form, but you can also use another form:!function () {} () before the number can be replaced by- +~ and so on unary operators, thus saving 1 bytes.
JS file in the pre-function plus a semicolon and exclamation mark what is the meaning?