An analysis of JavaScript automatic semicolon insertion (ASI) mechanism _javascript techniques

Source: Internet
Author: User

Objective

Trust in C # and Java everyone knows that the semicolon is used as a punctuation (eos,end of statement), and must be semicolon, otherwise the compilation will not pass. But JavaScript has an ASI mechanism that allows us to omit semicolons. ASI mechanism is not to say that in the parsing process, the parser automatically adds a semicolon to the code, but says that the parser in addition to the semicolon will be based on a certain rule for the basis of the rules, so as to ensure the correctness of the analysis.

Normative theory

The ES5 standard defines an automatic semicolon insertion rule that includes the following three basic rules plus two preconditions:

Predecessor condition

1. If the parsing result is an empty statement after inserting a semicolon, the semicolon will not be inserted automatically.

Example: (empty statement, else before the addition of good points)

if (a > B) 

2. If you insert a semicolon and it becomes for one of the two semicolons of the statement head, the semicolon is not inserted automatically.

Example: (No semicolon)

for (A; b 
)

Basic rules

Left to right parser, when you encounter a problem that does not conform to any grammatical generation token (called the offending token (offending token)), token automatically inserts a semicolon before the violation if one of the following conditions is true.

1, at least one LineTerminator split the violation token and the previous one token .

2, token the violation is}.

Example: (1, 2 does not conform to any of the production, and there is a lineterminator, so the violation token 2 before the bonus points, 2 and} is because the violation token is} so the semicolon)

{1
{1
; 2;} 3;

Left to right parser, the tokens input stream has ended, and when the parser cannot parse the input token stream into a single full ECMAScript program, the semicolon is automatically inserted at the end of the input stream.

For a limited generation, which is the following 5, we call the "LineTerminator" after the resulting [no] here] token token , if token token at least one lineterminator exists between and the restricted Then it will be automatically added before the limit is restricted token token .

Restricted production is limited to the following 5:

Postfixexpression:

lefthandsideexpression [No LineTerminator here] + + lefthandsideexpression [No lineterminator here]--

Continuestatement:

Continue [No lineterminator here] Identifier;

Breakstatement:

Break [No LineTerminator] Identifier;

Returnstatement:

return [No LineTerminator] Expression;

throwstatement: throw [No lineterminator here] Expression;

Induction

Avoid the problems caused by ASI

1, suffix operator + + or--and its operands should appear on the same line.

2, return or throw the expression at the beginning of the statement should be and return or the throw token same row.

3, break or the identifier of the continue statement should and break or the continue token same line.

When to add a semicolon

No semicolon party (lazy party) want to have no semicolon, then you need to know when to add a semicolon. An article on the Internet summed up NO ASI and there are several errors, in which case we want to add a semicolon. The following are the corresponding descriptions:

Before the statement ([/+-) preceded by a semicolon (because normal writing does not appear with the., *% as the beginning of the statement, so just remember the front 5, you can lazy lazy oh)

But here only consider the situation of the line, in fact, there is no change in ASI, this will be in accordance with the standards of the three rules!

Knowing this, we can actually omit most of the semicolon. But it is also not required, because it is based on personal habits and team style to go.

Small supplement

Why do I have to add a semicolon before I execute a function?

This is mainly due to the lack of semicolons in the case of code merging compression, and the error caused by this. Knowing the rules above, you can avoid errors by adding a semicolon to the front of the line.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.