首先,我应该问这是否依赖浏览器.
我已经读过,如果找到无效令牌,但代码部分在该无效令牌之前有效,如果在令牌之前有行休息,则在令牌之前插入一个分号.
但是,分号插入引用的bug的常见例子是:
return
_a+b;
..这似乎不遵循此规则,因为_a将是一个有效的令牌.
另一方面,打破呼叫链按预期工作:
$('#myButton')
.click(function(){alert("Hello!")});
有没有人对规则有更深入的描述?
首先,我应该问这是否依赖浏览器.
我已经读过,如果找到无效令牌,但代码部分在该无效令牌之前有效,如果在令牌之前有行休息,则在令牌之前插入一个分号.
但是,分号插入引用的bug的常见例子是:
return
_a+b;
..这似乎不遵循此规则,因为_a将是一个有效的令牌.
另一方面,打破呼叫链按预期工作:
$('#myButton')
.click(function(){alert("Hello!")});
有没有人对规则有更深入的描述?
首先,您应该知道哪些语句受自动分号插入的影响(也称为简洁的ASI):
var
语句 do-while
语句 continue
语句 break
语句 return
语句 throw
语句 ASI的具体规则,在规范 11.9.1 自动分号输入规则中描述
叙述了三个案件:
当遇到语法不允许的令牌(LineTerminator
或}
)时,在它之前插入一个分号,如果:
LineTerminator
。 }
例如:
{ 1
2 } 3
转换为
{ 1
;2 ;} 3;
NumericLiteral
1
符合第一个条件,以下令牌是行终止器。
2001年12月31日终了的两年期收入和支出及准备金和基金结余变动报表
2
满足第二个条件,以下令牌是}
.
当遇到令牌输入流的末尾并且解析器无法将输入令牌流作为一个完整的程序解析时,然后在输入流的末尾自动插入一个分号。
例如:
a = b
++c
转换为:
a = b;
++c;
这种情况发生在某些语法生成允许令牌的情况下,但生产是一个限制生产,在限制令牌之前自动插入一个分号。
限制性产品:
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
经典示例,使用ReturnStatement
:
return
"something";
转换为
return;
"something";