简体   繁体   中英

Javascript automatic semicolon insertion for do-while statements

Since ES6, a new case has been added for rule 1 of automatic semicolon insertion :

The previous token is ) and the inserted semicolon would then be parsed as the terminating semicolon of a do-while statement (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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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