简体   繁体   English

Javascript do-while 语句的自动分号插入

[英]Javascript automatic semicolon insertion for do-while statements

Since ES6, a new case has been added for rule 1 of automatic semicolon insertion :从 ES6 开始,为自动分号插入规则 1 添加了一个新的案例:

The previous token is ) and the inserted semicolon would then be parsed as the terminating semicolon of a do-while statement (13.7.2).前一个标记是 ),然后插入的分号将被解析为 do-while 语句 (13.7.2) 的终止分号。

This allows one to avoid line termination and write ugly code such as:这允许人们避免行终止并编写丑陋的代码,例如:

do {} while (false) var a = 42

What is the rationale behind this rule?这条规则背后的理由是什么? Some useful use-case?一些有用的用例?

I'm pretty sure that "case" added in ES2015 is only there to standardize rules which browsers had already implemented in order to be compatible with terribly-written (or weirdly minified) scripts.我很确定 ES2015 中添加的“案例”只是为了标准化浏览器已经实现的规则,以便与编写得非常糟糕(或奇怪地缩小)的脚本兼容。 It wasn't exactly a new feature, so much as it was a tweak of the specification to be in line with what browsers were doing already.它并不完全是一个功能,它只是对规范的调整,以符合浏览器已经在做的事情。

For example, your snippet runs in IE11, which was released in 2013:例如,您的代码片段在 2013 年发布的 IE11 中运行:

 do {} while (false) var a = 42; console.log('no parse errors');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM