Are semicolons needed after an object literal assignment in JavaScript?

前端 未结 7 1726
北海茫月
北海茫月 2020-12-03 16:38

The following code illustrates an object literal being assigned, but with no semicolon afterwards:

var literal = {
    say: function(msg) { alert(msg); }
}
l         


        
7条回答
  •  旧时难觅i
    2020-12-03 17:17

    In this case there is no need for a semicolon at the end of the statement. The conclusion is the same but the reasoning is way off.

    JavaScript does not have semicolons as "optional". Rather, it has strict rules around automatic semicolon insertion. Semicolons are not optional with statements like break, continue, or throw. Refer to the ECMA Language Specification for more details; specifically 11.9.1, rules of automatic semicolon insertion.

提交回复
热议问题