Are semicolons needed after an object literal assignment in JavaScript?

前端 未结 7 1724
北海茫月
北海茫月 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条回答
  •  星月不相逢
    2020-12-03 17:16

    The semi-colon is not necessary. Some people choose to follow the convention of always terminating with a semi-colon instead of allowing JavaScript to do so automatically at linebreaks, but I'm sure you'll find groups advocating either direction.

    If you are looking at writing "correct" JavaScript, I would suggest testing things in Firefox with javascript.options.strict (accessed via about:config) set to true. It might not catch everything, but it should help you ensure your JavaScript code is more compliant.

提交回复
热议问题