The semicolon insertion mechanism in JavaScript

Source: Internet
Author: User

Text: The semicolon insertion mechanism in JavaScript

Inserted only before, after one or more line breaks, and at the end of program input

This means that you can only omit semicolons in a row, a block of code, and where a program ends.

Which means you can write code like this.

function Square (x) {    var n = +x    return n * n}

But it can't be written like the code below, so it's an error.

function Area (r) {    r = +r    return math.pi*r*r}//error

Inserted only when subsequent input tags cannot be resolved

In other words, semicolon insertion is an error correction mechanism. Watch the code talk.

A = B (f ())// can parse correctly into a single statement  unit price in the following statement a == BF ()//  Resolved to two independent statements a = BF (); // Parsing Error

So you have to pay attention to the beginning of the next statement to determine if you can legally omit the semicolon.

(, [, + 、-、 and/ these five characters start with a statement, then it's best not to omit the semicolon before.)

For example, OH

A = b[' r ', ' G ', ' B '].foreach (function  (key) {    console.log (key);});

You thought there was no error, but the parser parsed it into the following statement

A = b[' R ', ' G ', ' B '].foreach (function  (key) {    console.log (key);});

Because the second sentence begins with [, so the parser does not automatically insert a semicolon after the first statement, so that it resolves to the above, b[' B '].foreach is not wrong when parsing the above equation?

So (, [, + 、-、 and/ these five characters start with the statement, then it's best not to omit the semicolon in front.)

To omit a semicolon, an experienced programmer will follow the statement with a declaration statement to ensure that the parser resolves correctly. As shown below

A = bvar x// intentionally adds a declaration statement to ensure that A = B does not and (f ()) resolve to a join (f ())

So if you need to omit the semicolon, you must check that the next line of the start tag causes the parser to disable the automatic insertion of the semicolon, or you can have a semicolon in the (, [, + 、-、, and/ five characters) above five characters.

Omitting semicolons causes script connection problems
// File1.js (function  () {    //... })///file2.js(function  () {    //..... })()

When the above two files are connected, they will be parsed as follows

(function  () {    //...}) () (function  () {    //...}) ()

So omitting the semicolon not only takes care of the next tag of the current file, but also beware of any markup that might appear after the script is connected to the statement.

To avoid parser parsing errors, you can have an extra semicolon in each file prefix to protect the script from careless connections. If the first statement of the file starts with the 5 vulnerable characters above, you should add an additional semicolon prefix.

JavaScript Syntax restriction production type

JavaScript syntax restriction generation: No line breaks are allowed between two characters.

To illustrate:

return {};

The above code is parsed into a

return ; {};

Semicolon insert rule for self-increment decrement operation
a+b

What do you think the code is going to be parsed into? Say the answer, because the increment operator can be both a predecessor and a post operator, but the post operator cannot appear before the line break, so the code above is parsed into

A; ++b;

Semicolons are not inserted automatically as delimiters in the header of a For loop empty statement
 for (var i = 0,total=1    < length    i+ +)    {total *=i;}

Like the above code, there is a parsing error.

The while of the empty loop body also needs to display the semicolon, otherwise it will also cause parsing errors

function mytest () {    while   (true)}

Must be written as follows in order not to error OH

function mytest () {       while (true);}

Sum up, please.
    1. The semicolon is deduced only before the} tag, at the end of the line, and at the end of the program
    2. The semicolon is deduced only when the immediately preceding token cannot be parsed.
    3. You must never omit a semicolon before a statement that starts with (, [, + 、-、, and/or characters
    4. When the script is connected, insert the semicolon explicitly between the scripts
    5. Must not be wrapped before return, throw, break, continue, + +, or--arguments
    6. A semicolon cannot be pushed out as a delimiter for a for loop's head or an empty statement

The semicolon insertion mechanism in JavaScript

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.