您是否建议在JavaScript中的每个语句后使用分号? [英] Do you recommend using semicolons after every statement in JavaScript?

查看:92
本文介绍了您是否建议在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程序表单的第一个到第n个 th 标记在语法上有效但是第一个通过n + 1 st 标记不是,并且n th 标记和n + 1 st 之间存在换行符令牌,然后解析器尝试在n th 和n + 1 st 令牌之间插入虚拟分号后再次解析程序。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆