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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:05:18  来源:igfitidea点击:

Use of semicolons in ES6

javascriptnode.jsecmascript-6

提问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,我个人认为添加分号不是什么大问题,所以我继续使用它们。