This question already has an answer here:
这个问题在这里已有答案:
- Do you recommend using semicolons after every statement in JavaScript? 11 answers
您是否建议在JavaScript中的每个语句后使用分号? 11个答案
I've only written a small amount of JavaScript that runs embedded in a Java application, but it was tested using QUnit, has been mixed, and I've not noticed any problems yet.
我只编写了少量运行嵌入Java应用程序的JavaScript,但它是使用QUnit测试的,已经混合了,我还没有注意到任何问题。
Is there some conventional wisdom whether to use semicolons or not in JavaScript?
是否在JavaScript中使用分号是否有一些传统的智慧?
8 个解决方案
#1
Use them. Use them constantly.
使用它们。经常使用它们。
It's far too easy to have something break later on because you neglected a semi-colon and it lost the whitespace which saved it before in a compression/generation/eval parse.
因为你忽略了一个分号并且它在压缩/生成/ eval解析之前丢失了保存它的空白,所以稍后容易破坏它太容易了。
#2
I'd say use them all the time; most code you'll encounter uses them, and consistency is your friend.
我会说他们一直都在使用它们;您遇到的大多数代码都会使用它们,而且一致性就是您的朋友。
#3
They are required by the ECMAscript standard, see section 7.9 - it's just that the standard defines some rules that allow them to be automatically inserted while parsing the script.
它们是ECMAscript标准所要求的,参见7.9节 - 只是标准定义了一些允许在解析脚本时自动插入的规则。
So always use them!
所以总是使用它们!
#4
If you don't use them and then minify your code you can run into issues with all your code being on a single line and the browser doesn't fully grasp which command ends where.
如果您不使用它们然后缩小代码,则可能会遇到问题,所有代码都在一行上,并且浏览器无法完全掌握哪个命令在哪里结束。
#5
I always promote the use of semi-colons when writing JavaScript. Often the interpreter will be able to infer them for you; but I have yet to see a reason (aside from laziness ;-)) why you would deliberately write your code in a less precise fashion than possible.
在编写JavaScript时,我总是提倡使用分号。通常,口译员可以为您推断它们;但我还没有看到一个原因(除了懒惰;-))为什么你会故意以不太可能的方式编写你的代码。
To my mind, if the structure of the code is obvious, it will be really clear where the semicolons go, such that you won't even have to think about it after getting in the habit (i.e. at the end of each line); on the other hand, if it's not immediately clear to you where the semicolon goes, then chances are the structure isn't the most obvious anyway, and explicit semicolons are needed there more than they would be elsewhere.
在我看来,如果代码的结构是显而易见的,那么分号的位置将会非常清楚,这样你甚至不必在养成习惯后(即每行末尾)考虑它;另一方面,如果你不能立即明白分号的位置,那么结构的可能性并不是最明显的,并且需要明确的分号比其他地方更多。
It also gets you into the habit of understanding and delimiting statements in your head, so you have a (admittedly marginally) better understanding of how your code might parse into an AST or similar. And that's got to be a good thing when debugging syntax errors.
它还让你习惯于理解和分隔你的头脑中的陈述,因此你有一个(不可否认的是)更好地理解你的代码如何解析成AST或类似的东西。在调试语法错误时,这是一件好事。
#6
Use them. There are a few reasons why, most notably
使用它们。最值得注意的原因有几个
- JavaScript minifiers / compressors
- Exceptions to the rule that a new line is a new expression (e.g. terminating a line with a variable and starting the next one with a parenthesis,
)
.)
JavaScript minifiers /压缩器
新行是新表达式的规则的例外(例如,用变量终止一行并用括号开始下一行)。)
#7
The semi colon triggers auto-indenting in my editor. Good enough reason for me to use it always.
半冒号在我的编辑器中触发自动缩进。足够的理由让我永远使用它。
And yes, consistency too.
是的,一致性也是如此。
#8
The basic idea of semicolons is to tell the browser that you have just finished a command. You should use them.
分号的基本思想是告诉浏览器你刚刚完成了一个命令。你应该使用它们。
#1
Use them. Use them constantly.
使用它们。经常使用它们。
It's far too easy to have something break later on because you neglected a semi-colon and it lost the whitespace which saved it before in a compression/generation/eval parse.
因为你忽略了一个分号并且它在压缩/生成/ eval解析之前丢失了保存它的空白,所以稍后容易破坏它太容易了。
#2
I'd say use them all the time; most code you'll encounter uses them, and consistency is your friend.
我会说他们一直都在使用它们;您遇到的大多数代码都会使用它们,而且一致性就是您的朋友。
#3
They are required by the ECMAscript standard, see section 7.9 - it's just that the standard defines some rules that allow them to be automatically inserted while parsing the script.
它们是ECMAscript标准所要求的,参见7.9节 - 只是标准定义了一些允许在解析脚本时自动插入的规则。
So always use them!
所以总是使用它们!
#4
If you don't use them and then minify your code you can run into issues with all your code being on a single line and the browser doesn't fully grasp which command ends where.
如果您不使用它们然后缩小代码,则可能会遇到问题,所有代码都在一行上,并且浏览器无法完全掌握哪个命令在哪里结束。
#5
I always promote the use of semi-colons when writing JavaScript. Often the interpreter will be able to infer them for you; but I have yet to see a reason (aside from laziness ;-)) why you would deliberately write your code in a less precise fashion than possible.
在编写JavaScript时,我总是提倡使用分号。通常,口译员可以为您推断它们;但我还没有看到一个原因(除了懒惰;-))为什么你会故意以不太可能的方式编写你的代码。
To my mind, if the structure of the code is obvious, it will be really clear where the semicolons go, such that you won't even have to think about it after getting in the habit (i.e. at the end of each line); on the other hand, if it's not immediately clear to you where the semicolon goes, then chances are the structure isn't the most obvious anyway, and explicit semicolons are needed there more than they would be elsewhere.
在我看来,如果代码的结构是显而易见的,那么分号的位置将会非常清楚,这样你甚至不必在养成习惯后(即每行末尾)考虑它;另一方面,如果你不能立即明白分号的位置,那么结构的可能性并不是最明显的,并且需要明确的分号比其他地方更多。
It also gets you into the habit of understanding and delimiting statements in your head, so you have a (admittedly marginally) better understanding of how your code might parse into an AST or similar. And that's got to be a good thing when debugging syntax errors.
它还让你习惯于理解和分隔你的头脑中的陈述,因此你有一个(不可否认的是)更好地理解你的代码如何解析成AST或类似的东西。在调试语法错误时,这是一件好事。
#6
Use them. There are a few reasons why, most notably
使用它们。最值得注意的原因有几个
- JavaScript minifiers / compressors
- Exceptions to the rule that a new line is a new expression (e.g. terminating a line with a variable and starting the next one with a parenthesis,
)
.)
JavaScript minifiers /压缩器
新行是新表达式的规则的例外(例如,用变量终止一行并用括号开始下一行)。)
#7
The semi colon triggers auto-indenting in my editor. Good enough reason for me to use it always.
半冒号在我的编辑器中触发自动缩进。足够的理由让我永远使用它。
And yes, consistency too.
是的,一致性也是如此。
#8
The basic idea of semicolons is to tell the browser that you have just finished a command. You should use them.
分号的基本思想是告诉浏览器你刚刚完成了一个命令。你应该使用它们。