gpt4 book ai didi

javascript - 这里为什么需要分号?

转载 作者:行者123 更新时间:2023-12-03 06:00:53 26 4
gpt4 key购买 nike

我只是使用 es6 重新分配变量 a 和 b。如果我在 a 和 b 声明和赋值语句中省略分号,为什么会出现错误?如果省略分号,解析器是否会尝试从 2 中提取属性?让 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]

我正在寻找失败的实际原因。谢谢!

最佳答案

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

是的。如果未明确以分号结束语句,则无法安全地以 [ 开始语句。

这在很多地方都有写到,这是 standardJS(或其他 linter)会警告您的众多事情之一:http://standardjs.com/rules.html#semicolons

关于javascript - 这里为什么需要分号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39753755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com