为什么比较{}和[]会显示错误? [英] Why does comparing {} and [] show an error?

查看:60
本文介绍了为什么比较{}和[]会显示错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在空闲时间,我只是在玩js控制台,却遇到了意外错误:

On my free time, I was just playing with js console, I got an unexpected error:

js> [] == {}
false

js> {} == []
typein:5: SyntaxError: syntax error:

我尝试了===:

js> [] === {}
false
js> {} === []
typein:9: SyntaxError: syntax error:

在这里想错了吗?

我在Firefox,Chrome和jscore上对此进行了测试.

I tested this with Firefox, Chrome and jscore.

推荐答案

这是因为在第二种情况下,{}被解释为代码块,而不是对象.

That's because in the second case, {} is interpreted as a code block, rather than an object.

如果您尝试({}) == [],则效果很好.

If you try ({}) == [] it works just fine.

js> ({}) == []
false
js> ({}) === []
false

这篇关于为什么比较{}和[]会显示错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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