您是否建议在 JavaScript 中的每个语句后使用分号? [英] Do you recommend using semicolons after every statement in JavaScript?
问题描述
在许多情况下,JavaScript 解析器会为您插入分号,如果您将它们排除在外.我的问题是,您是否将它们排除在外?
In many situations, JavaScript parsers will insert semicolons for you if you leave them out. My question is, do you leave them out?
如果您不熟悉规则,Mozilla 网站.这是关键点:
If you're unfamiliar with the rules, there's a description of semicolon insertion on the Mozilla site. Here's the key point:
如果 JavaScript 程序形式的第一个到第 nth 个标记在语法上有效,但第一个到 n+1st 个标记无效,并且有一行在第 nth 个标记和 n+1st 个标记之间中断,然后解析器在第 nth 个标记之间插入虚拟分号标记后再次尝试解析程序 和 n+1st 个标记.
If the first through the nth tokens of a JavaScript program form are grammatically valid but the first through the n+1st tokens are not and there is a line break between the nth tokens and the n+1st tokens, then the parser tries to parse the program again after inserting a virtual semicolon token between the nth and the n+1st tokens.
那个描述可能不完整,因为它没有解释@Dreas 的例子.任何人都有完整规则的链接,或者看看为什么这个例子有一个分号?(我在 JScript.NET 中尝试过.)
That description may be incomplete, because it doesn't explain @Dreas's example. Anybody have a link to the complete rules, or see why the example gets a semicolon? (I tried it in JScript.NET.)
这个stackoverflow问题是相关的,但只讨论特定场景.
This stackoverflow question is related, but only talks about a specific scenario.
推荐答案
是的,您应该在 JavaScript 中的每个语句之后使用分号.
Yes, you should use semicolons after every statement in JavaScript.
这篇关于您是否建议在 JavaScript 中的每个语句后使用分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!