Why is a semicolon needed here? [duplicate]

寵の児 提交于 2021-02-04 08:24:07

问题


I am simply using es6 to reassign variables a & b. Why do I get an error if I leave the semicolon off of the a and b declaration and assignment statements? Does the parser try to pull a property out of 2 if the semicolon is left off? let b = 2[a, b]...?

Works:
let a = 1;
let b = 2;
[a, b] = [b, a]

Error:

let a = 1
let b = 2
[a, b] = [b, a]

I am looking for the actual reason this fails. Thanks!


回答1:


Does the parser try to pull a property out of 2 if the semicolon is left off? let b = 2[a, b]...?

Yes. You cannot safely start a statement with a [ if you're not explicitly ending statements with semicolons.

This has been written about in many places, and it's one of the many things that standardJS (or other linters) will warn you about: http://standardjs.com/rules.html#semicolons



来源:https://stackoverflow.com/questions/39753755/why-is-a-semicolon-needed-here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!