Javascript 最后需要分号吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4002234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do we need semicolon at the end?
提问by kobe
I missed semicolons in some of the places in my JavaScript, but its not throwing error in any of the browsers. Is the ;
at the end needed?
我在 JavaScript 的某些地方错过了分号,但它不会在任何浏览器中引发错误。是;
在最后需要的?
采纳答案by John K
The concept is known as JavaScript Semicolon Insertionor "Automatic Semicolon Insertion". This blog post: JavaScript Semicolon Insertion: Everything you need to knowoutlines the concept well in an understandable manner using examples under the headings:
这个概念被称为JavaScript 分号插入或“自动分号插入”。这篇博文:JavaScript Semicolon Insertion: Everything you need to know使用标题下的示例以易于理解的方式很好地概述了这个概念:
- Where Semicolons are Allowed
- Where Semicolons May be Omitted
- The rules
- 允许使用分号的地方
- 可以省略分号的地方
- 规则
It even digs into the official ECMAScript specificationabout the topic.
它甚至深入研究了有关该主题的官方 ECMAScript 规范。
回答by ShZ
Javascript does something called "semicolon insertion" which means you can actually write code that omits the semicolon in certain places, and they'll basically be added for you when the code is parsed.
Javascript 做了一些称为“分号插入”的事情,这意味着您实际上可以编写在某些地方省略分号的代码,并且在解析代码时基本上会为您添加分号。
The rules around when this happens a little complex. For simplicity's sake, many developers simply pretend semicolon insertion doesn't exist.
发生这种情况时的规则有点复杂。为简单起见,许多开发人员只是假装分号插入不存在。
回答by Pylinux
You canwrite javascript without semicolon, you only need to insert them if you start a line with a parantesis (
or a bracket [
.
您可以编写没有分号的 javascript,您只需要在以 parantesis(
或括号开头的行中插入它们[
。
The sugarjstimes()
function is a good example:
该sugarjstimes()
功能是一个很好的例子:
<script>
var somthing = 1 + 3
;(5).times(function(n){
console.log(n + " line") //prints "X line" 5 times
})
</script>
This article debunks most of the myths surounding javascript and semicolons: link
这篇文章揭穿了围绕 javascript 和分号的大多数神话:链接
回答by Phil Bair
To say that writing code with semicolons makes it more readable is absurd. It makes your code more CLUTTERED. Look at the code on this page without the semicolons and tell me it's less readable. It's more readable, less cluttered, cleaner and more elegant. Semicolons are ugly and unnecessary. See this article: http://mislav.uniqpath.com/2010/05/semicolons/
说用分号编写代码使其更具可读性是荒谬的。它使您的代码更加混乱。查看此页面上没有分号的代码,并告诉我它的可读性较差。它更具可读性,更简洁,更干净,更优雅。分号是丑陋的和不必要的。见这篇文章:http: //mislav.uniqpath.com/2010/05/semicolons/
回答by Necronet
Semicolons are not required for JavaScript programming, nevertheless I advice you to use it. It makes your code more readable and is actually a good practice, and almost all cool programming languages uses it.
JavaScript 编程不需要分号,但我建议您使用它。它使您的代码更具可读性,实际上是一种很好的做法,几乎所有很酷的编程语言都使用它。
Take a stand and use it, it's up to you now!
站出来使用它,现在就看你了!
回答by Daniel G. Wilson
If it's not throwing compiler errors, you should be fine. It's better that you do remember to use them all the time however, as some languages that you might get into such as objective-c are adamant about their use.
如果它没有抛出编译器错误,你应该没问题。但是,最好记住始终使用它们,因为您可能会接触到的某些语言(例如 Objective-c)坚持使用它们。