JavaScript 对象和字符串之间的相等性

发布于 2024-09-13 05:11:04 字数 258 浏览 5 评论 0原文

根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜吻♂芭芘 2024-09-20 05:11:04

它将左侧数组的字符串表示形式与右侧的字符串进行比较。

alert(['', ''] == ','); // true

alert([1, 2] == '1,2'); // true

当然,您可以使用严格比较运算符来避免这种情况......

alert([''] === ''); // false

It's comparing the string representation of the array on the left to the string on the right.

alert(['', ''] == ','); // true

alert([1, 2] == '1,2'); // true

Of course you can use the strict comparison operator to avoid this...

alert([''] === ''); // false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文