JavaScript open brace in the same line

后端 未结 6 1575
心在旅途
心在旅途 2020-12-16 03:50

I remember there is a convention/recommendation to put opening brace in the same line, because of the way JavaScript adds a semicolon or something.

//OK
fun         


        
6条回答
  •  孤城傲影
    2020-12-16 04:25

    The JavaScript Garden has a chapter about automatic semicolon insertion. It gives good examples when semicolons are added automatically:

    JavaScript is not a semicolon-less language, it in fact needs the semicolons in order to understand the source code. Therefore the JavaScript parser automatically inserts them whenever it encounters a parse error due to a missing semicolon.

    In your example JavaScript wouldn't encounter an error due to a missing semicolon though.

提交回复
热议问题