<分区>
在 Mac Chrome JS 控制台中,我遇到以下结果:
{} 评估为 Object()
{};评估为未定义
分号如何影响分号之前的内容?
这与ASI有关,还是其他解析规则?
标签 javascript
<分区>
在 Mac Chrome JS 控制台中,我遇到以下结果:
{} 评估为 Object()
{};评估为未定义
分号如何影响分号之前的内容?
这与ASI有关,还是其他解析规则?
最佳答案
Chrome 实际上评估 {}
就像您编写了 ({})
,因此强制解释器将 is 视为对象字面量而不是空 block ;
{
console.log("the line starts with { so it's treated is an empty block, not an object");
}
({}) //this is an object that is discarded after the line is read
为了说明它实际上是一个空 block ,请看这段代码:
{} == {} //syntax error because the effective code is "== {}"
并与这段代码进行比较
({}) == {} //force the left side to be an object
在上面,没有错误,因为它被正确评估然后结果被丢弃。
然而,下面是一个语法错误,所以在那种情况下,Chrome 会默默地去掉大括号
({};)
关于javascript - 为什么做 {} 和 {};在 JS 中评估不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55080703/