一行开头是括号或者方括号的时候,末尾必须要加分号。
以下代码会报错:
(function(){ console.log(1)}())
(function(){ console.log(2)}())

这种情况要加分号:
(function(){ console.log(1)}());
(function(){ console.log(2)}())
至于js 到底要不要写分号,仁者见仁,智者见智,按自己习惯来就好了。
最后附上尤雨溪关于js要不要写分号的观点:

一行开头是括号或者方括号的时候,末尾必须要加分号。
以下代码会报错:
(function(){ console.log(1)}())
(function(){ console.log(2)}())
这种情况要加分号:
(function(){ console.log(1)}());
(function(){ console.log(2)}())
至于js 到底要不要写分号,仁者见仁,智者见智,按自己习惯来就好了。
最后附上尤雨溪关于js要不要写分号的观点:
本文标题:js 必须要加分号的场景
本文链接:https://www.haomeiwen.com/subject/cbhdcltx.html
网友评论