为什么在这里需要分号? [英] Why is a semicolon needed here?
问题描述
我只是使用es6重新分配变量a&b.如果在a和b声明和赋值语句中保留分号,为什么会出现错误?如果不使用分号,解析器是否会尝试从2中提取属性?让b = 2 [a,b] ...?
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!
推荐答案
如果不使用分号,解析器是否尝试从2中提取属性?让b = 2 [a,b] ...?
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.
这是在许多地方写的,这是standardJS(或其他linters)会警告您的许多事情之一: http://standardjs.com/rules.html#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
这篇关于为什么在这里需要分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!