JavaScript 对象和字符串之间的相等性
根据 Firebug 控制台,我们在 JavaScript 中有以下内容:
>>> [''] == ''
true
>>> [''] == ['']
false
发现 Python 在这里更符合逻辑,我希望它是反过来的。不管怎样,我可以理解第二个——显然两个不同的对象永远不会彼此比较相等——但是第一个给出 true 的原因是什么? ['', '']
比较等于什么字符串?
According to Firebug console, we have the following in JavaScript:
>>> [''] == ''
true
>>> [''] == ['']
false
Finding Python to be much more logical here, I'd expect it to be the way round. Anyway, I can understand the second one — apparently two different objects never compare equal to each other, — but what is the reason for the first to give true
? What string would ['', '']
compare equal to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它将左侧数组的字符串表示形式与右侧的字符串进行比较。
当然,您可以使用严格比较运算符来避免这种情况......
It's comparing the string representation of the array on the left to the string on the right.
Of course you can use the strict comparison operator to avoid this...