JS from the executive function before adding a semicolon (;!) What does the function () {} ();) mean? __ Data structure

Source: Internet
Author: User

;(function () {
//This should be a plugin for a system JS
//Specific function code ...
}) ();

From the syntax, the semicolon in JavaScript denotes the end of the statement, which, at the beginning, may be separated from other methods for compression, indicating the beginning of a new statement. Therefore, if in a separate JS file, the beginning of the semicolon is meaningless, you can delete.

The exclamation point is a logical operator, which means "!true", which is common in this way if ({}) {}, and the operator-loaded function is defined as a whole, then the function is called, and the returned structure is logically calculated.

To put it plainly, this is the construction of an immediate executive function, suggested that the landlord know the meaning can be, write their own time, you can use a clearer format:

(function () {}) ();


This is mainly due to the lack of semicolons in the case of code merging compression, and the error caused by this.
This is in fact at present generally based on lexical parsing to compress (UGLIFYJS, etc.) under the general trend, is not necessary, so understand why to do so is to avoid the key to dogma


1.
(function () {alert ("1")}) ()
(function () {alert ("2")}) ()
Error

2.
(function () {alert ("1")}) ();
(function () {alert ("2")}) ()
Correctly without semicolons separating the functions next to each other, there will be an error. Should be related to the analysis of JS.

It has nothing to do with the religious warfare of semicolons, mainly because JS has the problem of hiding the ASI (automatic semicolon insertion) mechanism. Say, correctly, the execution result of the following code. (I already gave a hint ~)

var test=function (a) {
    alert (a) return
    function (c) {
        alert (c)
    }
} (function (b) {
    Console.log (b)
}) (1)
Write Iife, the implementation of the body before the active plus semicolon, the above code is a pit ... (a single code file can be tested through, but multiple JS files are merged after the exception is run)
var test=function (a) {
    alert (a) return
    function (c) {
        alert (c)
    }
};(function (b) {
    Console.log (b)
}) (1)
Because the syntax tree generated by these two pieces of code is completely different. For details please see esprima:parser,ast too long, here is not posted.

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.