问题描述
为什么下面的语句在JavaScript中返回false?
Why does the following statement return false in JavaScript?
new String('hello') === new String('hello')
推荐答案
两个字符串 objects 始终彼此不相等。请注意,JavaScript具有字符串 primitive 值以及用于创建包装器对象的String构造函数。所有对象相等性比较(特别是与 ===
的比较)均作为对引用相等性的测试。当然,对两个不同对象的引用永远不会相等。
Two String objects will always be unequal to each other. Note that JavaScript has string primitive values as well as a String constructor to create wrapper objects. All object equality comparisons (especially with ===
) are carried out as a test for reference equality. References to two different objects will of course never be equal to each other.
所以 hello === hello
将为 true
,因为它们是字符串基元。
So "hello" === "hello"
will be true
because those are string primitives.
这篇关于为什么新的String('hello')===新的String('hello')评估为False?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!