javascript - 为什么 Firefox 会针对文字 {"a": 1} while Chrome accepts it? 抛出错误

标签 javascript google-chrome firefox console syntax-error

在 Firefox 控制台中,由于冒号,它会引发错误:

{"a": 1}

SyntaxError: unexpected token: ':'

在 Chrome 控制台中它接受它:

{"a": 1}
{a: 1}

我知道两者都接受 {a: 1},我想知道为什么 Firefox 会因字符串 key 版本而卡住。

最佳答案

参见

Why is {} + {} no longer NaN in Chrome console?

Chrome devtools now automatically wrap everything that begins with { and ends with } in an implicit pair of parentheses (see code), to force its evaluation as an expression.

Firefox 不会这样做。在火狐浏览器中,

{a: 1}

被评估为一个 block ,其具有 label a,带有未使用的表达式 1:

{
  a:
  1
}

(这就是为什么在 FF 中,您会看到计算的最终表达式为 1:

enter image description here

)

但是标签不能包含在字符串分隔符中(标签只需具有纯标识符,就像变量一样),因此将 a: 更改为 "a": 抛出错误,因为冒号只能在标签后面或对象中的键值对之间进行解析。

关于javascript - 为什么 Firefox 会针对文字 {"a": 1} while Chrome accepts it? 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56617531/

相关文章:

javascript - 向 Jquery 提供非匿名函数 : success + additional confusion

javascript - file.value 返回未定义

javascript - 修复了 div 上的导航效果

google-chrome - Chrome开发工具控制台如何访问网站资源?

javascript - 找出 JS 中的哪个文件/行正在更改元素的文本

CSS:为什么容器上的填充或边框会改变封闭 float 的行为?

IE8、Chrome、Firefox 和 Safari 之间的 CSS 兼容性

css - mozilla,用第二个新样式更改第一个 css 样式时遇到问题

javascript - 使用单个新事件覆盖绑定(bind)到元素的所有 JavaScript 事件

javascript - 无法在 Chrome 扩展程序中运行内容脚本