为什么(“ foo”= = = new String (“ foo”))在 JavaScript 中计算为 false?

我本来打算在比较字符串值时一直使用 = = = (三重等于,严格比较) ,但现在我发现了这一点

"foo" === new String("foo")

是错误的,这一点也一样:

var f = "foo", g = new String("foo");
f === g; // false

当然:

f == g; // true

那么是建议始终使用 = = 进行字符串比较,还是始终在比较之前将变量转换为字符串?

7465 次浏览

"foo" is a string primitive. (this concept does not exist in C# or Java)

new String("foo") is boxed string object.

The === operator behaves differently on primitives and objects.
When comparing primitives (of the same type), === will return true if they both have the same value.

When comparing objects, === will return true only if they refer to the same object (comparing by reference). Thus, new String("a") !== new String("a").

In your case, === returns false because the operands are of different types (one is a primitive and the other is an object).


Primitives are not objects at all.
The typeof operator will not return "object" for primitives.

When you try to access a property of a primitive (using it as an object), the Javascript language will box it to an object, creating a new object every time. This is described in the specification.

This is why you cannot put properties on primitives:

var x = "a";
x.property = 2;
alert(x.property) //undefined

Each time you write x.property, a different boxed String object is created.

foo is the pure string and new String("foo") is the Object String

Using ===,

  • an Object is never equal to anything except another reference to itself.

  • a primitive is equal when compared to another primitive if their type and value are the same.

The new word is a criminal here (as usual, may I say)...

When you use new, you explicitly express your desire to work with object. It might be surprising for you, but this:

var x = new String('foo');
var y = new String('foo');
x === y;

... will give you a mighty false. It's simple: compared are not the objects' insides, but the objects' references. And they, of course, are not equal, as two different objects were created.

What you probably want to use is conversion:

var x = String('foo');
var y = String('foo');
x === y;

... and that will give you, as expected, true as result, so you can rejoice and prosper with your equal foos forever. )

From the node.js REPL ("node" on the command-line if installed):

> "foo" === new String("foo").valueOf()
true
> "foo" === new String("foo")
false
> typeof "foo"
'string'
> typeof new String("foo")
'object'
> typeof new String("foo").valueOf()
'string'