Javascript ES6中分号的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34950322/
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
Use of semicolons in ES6
提问by seven11
I was under the impression semicolons became obsolete with ES6. However, I came across this today:
我的印象是分号在 ES6 中已经过时了。然而,我今天遇到了这个:
Doesn't work:
不起作用:
let i = 0
[0, 1, 2, 3, 4, 5, 6].forEach(item => console.log(item))
Works:
作品:
let i = 0;
[0, 1, 2, 3, 4, 5, 6].forEach(item => console.log(item))
Why is the semicolon necessary here, and when should I use them?
为什么这里需要分号,我应该什么时候使用它们?
采纳答案by Evers
Without the semicolon [1,2,3,4,5,6] will be evaluated as property access. Which is perfectly fine JS, I personally don't think that adding semicolons is such a big deal so I keep using them.
没有分号 [1,2,3,4,5,6] 将被评估为属性访问。这是非常好的 JS,我个人认为添加分号不是什么大问题,所以我继续使用它们。