如果我在控制台中运行它,为什么会出现语法错误?{} === {} [英] Why do I get a syntax error if I run this in the console? {} === {}

查看:32
本文介绍了如果我在控制台中运行它,为什么会出现语法错误?{} === {}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Chromium 控制台中,我运行 {} === {} 并且出现语法错误,意外的 '==='.

In the chromium console I run {} === {} and I get a Syntax error, unexpected '==='.

如果我把它包装在括号中,比如 ({} === {}) 然后我得到 false,这是我所期望的.

If I however wrap this in parens, like ({} === {}) then I get false, what I'd expect.

第一个位置的对象字面量是否与代码块或其他东西混淆?

Is an object literal, in the first position, confused with a code block or something?

推荐答案

如果没有括号,{} 将被视为空的 代码块 javascript.= 后跟 代码块 将是无效的语法.这就是您在那里看到错误的原因.

Without a surrounding parenthesis, {} would be considered as an empty code block in javascript. = followed by a code block would be an invalid syntax. That is why you are seeing an error there.

如果你把它包裹在一个像 ({} === {}) 这样的括号里,那么它会被认为是一个 expression 并且它会被评估为false,因为两者都引用了两个不同的对象.

If you wrap it inside of a parenthesis like ({} === {}), then it would be considered as an expression and it will be evaluated to false as both are referencing two different objects.

下面的例子可能会让你清楚地了解它,

The following example may give you a clear picture about it,

{ var x = 5; console.log(x); } == 2
// will throw the same error that you are facing.

这篇关于如果我在控制台中运行它,为什么会出现语法错误?{} === {}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆