The semicolon insertion mechanism in javascript detail _ basics

Source: Internet
Author: User
Tags parse error

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

That means you can only omit a semicolon in one line, a block of code, and the end of a program.

Which means you can write the following code

Copy Code code as follows:

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

But can not write the same as the following code, so that the error-OH
Copy Code code as follows:

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

Inserted only if subsequent input tags cannot be resolved

That is, semicolon insertion is an error correction mechanism. Read the code and talk.

Copy Code code as follows:

A = b
(f ())
can be correctly resolved into a separate statement unit price in the following statement
A = B (f ())

A = b
F ()
is parsed into two separate statements
A = BF ();//Parse Error

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

(, [, + 、-、, and/These five characters start statements, then it is best not to omit the semicolon.)

For example, OH.

Copy Code code as follows:

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

You thought there was no mistake, but the parser was parsed into the following statement
Copy Code code as follows:

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

Because the second statement starts with [, so the parser does not automatically insert a semicolon after the first statement, so it resolves to b[' B '].foreach, as shown above, is not wrong?

So (, [, + 、-、 and/These five characters start statements, then it is best not to omit the semicolon.

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

Copy Code code as follows:

A = b
var x//intentionally added a declaration statement to ensure that A = B does not parse together with (f ())
(f ())

So if you need to omit the semicolon, you must check whether the next line start tag is the five characters that cause the parser to disable the automatic insertion of semicolons, or you can also place a semicolon in (, [, + 、-、, and/these five characters before

Omitting a semicolon causes a scripting connection problem

Copy Code code as follows:

File1.js
(function () {
//......
})()

File2.js
(function () {
//......
})()

When the above two files are connected, they are parsed into the following

Copy Code code as follows:

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

So omitting a semicolon requires careful care not only for the next tag of the current file, but also for any markup that might appear after the script is connected.

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

JavaScript syntax restriction generation

JavaScript syntax restriction generation: line wrapping is not allowed between two characters.

An example is provided:

Copy Code code as follows:

Return
{};

The above code is parsed into
Copy Code code as follows:

Return
{}
;

Semicolon insertion rule for self-subtraction operations

Copy Code code as follows:

A
++
B

Think about what the code would be like? Tell the answer, because the self-add operator can be both a front operator and a back operator, but the post operator cannot appear before the line break, so the code above is parsed into
Copy Code code as follows:

A
++b;

Semicolons are not inserted automatically as delimiters in the For loop empty statement's head

Copy Code code as follows:

for (var i = 0,total=1
I < length
i++) {
Total*=i;
}

There will be parsing errors like the code above.

The null loop body's while also needs a semicolon to display, otherwise it can cause parsing errors

Copy Code code as follows:

function MyTest () {
while (true)
}

Must be written as follows to make no error.
Copy Code code as follows:

function MyTest () {
while (true);
}

Well, to sum up.

1. Deduction of semicolons only before, at the end of a line, and at the end of a program
2. Deduce a semicolon only if the next tag cannot be parsed
3. Never omit a semicolon before a statement that begins with (, [, + 、-、, or characters)
4. When the script is connected, the semicolon is inserted explicitly between the scripts
5. Must not wrap before return, throw, break, continue, + + or--
6. Semicolons cannot be pushed out as the head of a for loop or the delimiter of an empty statement

Related Article

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.