A detailed description of the semicolon Insertion Mechanism in JavaScript
This article mainly introduces the semicolon Insertion Mechanism in JavaScript. This article describes the semicolon Insertion Mechanism in various cases in JavaScript. For more information, see
Inserted only before}, after one or more line breaks, and end of program input
That is to say, you can only omit semicolons in one line, one code block, and the end of a program.
In other words, you can write the following code:
The Code is as follows:
Function square (x ){
Var n = + x
Return n * n
}
But it cannot be written like the following code, so an error is reported.
The Code is as follows:
Function area (r) {r = + r return Math. PI * r} // error
Insert only when the subsequent input mark cannot be parsed
That is to say, semicolon insertion is an error correction mechanism. Read code
Copy the Code as follows:
A = B
(F ())
// The unit price of a separate statement can be correctly parsed in the following statement
A = B (f ())
A = B
F ()
// The statement is parsed into two independent statements.
A = bf (); // parsing error
Therefore, you must pay attention to the beginning of the next statement to determine whether you can legally omit the semicolon.
(, [, +,-, And/begin with a semicolon.
For example
The Code is as follows:
A = B
['R', 'G', 'B']. forEach (function (key ){
Console. log (key );
});
You thought there was no error, but the parser was resolved to the following statement:
The Code is as follows:
A = B ['R', 'G', 'B']. forEach (function (key ){
Console. log (key );
});
Because the second statement starts with [, the parser will not automatically insert a semicolon after the first statement, so it will be parsed as shown above. In the above formula, B ['B']. isn't forEach wrong?
Therefore, it is best not to omit the semicolon before the statements starting with (, [, +,-, And.
If you want to omit the semicolon, experienced programmers will follow the statement with a declaration statement to ensure that the parser is correctly parsed. As shown below
The Code is as follows:
A = B
Var x // specifically adds a declaration statement here to ensure that a = B will not be resolved together with (f ()
(F ())
Therefore, if you need to omit the semicolon, you must check whether the next line starts to mark the above five characters. As a result, the parser will disable automatic insertion of the semicolon, alternatively, you can add a semicolon before the five characters (, [, +,-, And /).
Script connection problems caused by skipping semicolons
The Code is as follows:
// File1.js
(Function (){
//......
})()
// File2.js
(Function (){
//......
})()
When the above two files are connected, they will be parsed as follows:
The Code is as follows:
(Function (){
//......
}) (Function (){
//......
})()
To omit the semicolon, you must be careful not only with the next mark of the current file, but also with any mark that may appear after the script connection after the statement.
To avoid parser parsing errors, You Can prefix each file with an additional semicolon to protect the script from careless connections. If the initial statement of the file describes the above five vulnerable characters, you should add an additional semicolon prefix.
JavaScript syntax limit generation
JavaScript syntax restriction Syntax: line breaks between two characters are not allowed.
Example:
The Code is as follows:
Return
{};
The above code is parsed
The Code is as follows:
Return;
{}
;
Semi-colon insertion rule for auto-increment and auto-subtraction operations
The Code is as follows:
A
++
B
Think about what the above Code will be parsed? The self-incrementing operator can be both a pre-operator and a post-operator, but the post-operator cannot appear before the line feed, so the above Code is parsed
The Code is as follows:
A;
+ + B;
The semicolon is not automatically inserted as a separator in the header of the for Loop null statement.
The Code is as follows:
For (var I = 0, total = 1
I <length
I ++ ){
Total * = I;
}
The above code will cause a parsing error.
The while of the empty loop body also needs to be displayed with a semicolon; otherwise, parsing errors may occur.
The Code is as follows:
Function mytest (){
While (true)
}
No error will be reported only when it is written as follows.
The Code is as follows:
Function mytest (){
While (true );
}