JS file in the pre-function plus a semicolon and exclamation mark what is the meaning?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.