Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

javascript - JavaScript自动分号(ASI)的规则是什么?(What are the rules for JavaScript's automatic semicolon insertion (ASI)?)

Well, first I should probably ask if this is browser dependent.

(好吧,首先我应该问一下这是否与浏览器有关。)

I've read that if an invalid token is found, but the section of code is valid until that invalid token, a semicolon is inserted before the token if it is preceded by a line break.

(我已经读到,如果找到了无效的令牌,但是代码段在该无效令牌之前一直有效,如果在该令牌之前加了换行符,则在该令牌之前插入一个分号。)

However, the common example cited for bugs caused by semicolon insertion is:

(但是,引用由分号插入引起的错误的常见示例是:)

return
  _a+b;

..which doesn't seem to follow this rule, since _a would be a valid token.

(..似乎不遵循此规则,因为_a是有效令牌。)

On the other hand, breaking up call chains works as expected:

(另一方面,分解呼叫链可以按预期工作:)

$('#myButton')
  .click(function(){alert("Hello!")});

Does anyone have a more in-depth description of the rules?

(是否有人对规则有更深入的描述?)

  ask by T.R. translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First of all you should know which statements are affected by the automatic semicolon insertion (also known as ASI for brevity):

(首先,您应该知道哪些语句受自动分号插入(为简洁起见也称为ASI)的影响:)

  • empty statement

    (空语句)

  • var statement

    (var陈述)

  • expression statement

    (表达陈述)

  • do-while statement

    (do-while声明)

  • continue statement

    (continue声明)

  • break statement

    (break声明)

  • return statement

    (return声明)

  • throw statement

    (throw声明)

The concrete rules of ASI, are described in the specification §11.9.1 Rules of Automatic Semicolon Insertion

(有关ASI的具体规则,请参见规范§11.9.1自动分号插入规则)

Three cases are described:

(描述了三种情况:)

  1. When a token ( LineTerminator or } ) is encountered that is not allowed by the grammar, a semicolon is inserted before it if:

    (当遇到语法不允许的标记( LineTerminator} )时,如果出现以下情况,则会在该标记前插入分号:)

    • The token is separated from the previous token by at least one LineTerminator .

      (令牌由至少一个LineTerminator与先前的令牌分隔。)

    • The token is }

      (令牌为})

    eg :

    (例如 :)

     { 1 2 } 3 

    is transformed to

    (转化为)

     { 1 ;2 ;} 3; 

    The NumericLiteral 1 meets the first condition, the following token is a line terminator.

    (NumericLiteral 1满足第一个条件,以下标记是行终止符。)


    The 2 meets the second condition, the following token is } .

    (2满足第二个条件,以下标记为} 。)

  2. When the end of the input stream of tokens is encountered and the parser is unable to parse the input token stream as a single complete Program, then a semicolon is automatically inserted at the end of the input stream.

    (当遇到令牌输入流的末尾并且解析器无法将输入令牌流作为单个完整程序解析时,则分号会自动插入到输入流的末尾。)

    eg :

    (例如 :)

     a = b ++c 

    is transformed to:

    (转换为:)

     a = b; ++c; 
  3. This case occurs when a token is allowed by some production of the grammar, but the production is a restricted production , a semicolon is automatically inserted before the restricted token.

    (如果某种语法的生产允许使用令牌,但这种生产是受限生产 ,则会在受限令牌之前自动插入分号。)

    Restricted productions:

    (限量生产:)

     UpdateExpression : LeftHandSideExpression [no LineTerminator here] ++ LeftHandSideExpression [no LineTerminator here] -- ContinueStatement : continue ; continue [no LineTerminator here] LabelIdentifier ; BreakStatement : break ; break [no LineTerminator here] LabelIdentifier ; ReturnStatement : return ; return [no LineTerminator here] Expression ; ThrowStatement : throw [no LineTerminator here] Expression ; ArrowFunction : ArrowParameters [no LineTerminator here] => ConciseBody YieldExpression : yield [no LineTerminator here] * AssignmentExpression yield [no LineTerminator here] AssignmentExpression 

    The classic example, with the ReturnStatement :

    (经典示例,带有ReturnStatement :)

     return "something"; 

    is transformed to

    (转化为)

     return; "something"; 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...