Why does new String('hello') === new Strin

2019-02-02 19:58发布

Why does the following statement return false in JavaScript?

new String('hello') === new String('hello')

7条回答
孤傲高冷的网名
2楼-- · 2019-02-02 20:12

Your code essentially says "Take a piece of paper and write 'hello' on it. Take another piece of paper and write 'hello' on that. Are they the same piece of paper?"

查看更多
一纸荒年 Trace。
3楼-- · 2019-02-02 20:13
typeof(new String()) === 'object';
==> true

(new Object()) === (new Object());
==> false

Any "object" structure in the "Heap" is unique;

Heap vs. Stack

查看更多
Bombasti
4楼-- · 2019-02-02 20:22

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.

So "hello" === "hello" will be true because those are string primitives.

查看更多
我想做一个坏孩纸
5楼-- · 2019-02-02 20:27

It evaluates to false because you're comparing two different objects: new will create a new object.

Related post: What is the 'new' keyword in JavaScript? Which explains in its (extensive) answer:

It [new] is 4 things:

  1. It creates a new object. The type of this object, is simply object.
  2. It sets this new object's internal, inaccessible, [[prototype]] property to be the constructor function's external, accessible, prototype object (every function object automatically has a prototype property).
  3. It executes the constructor function, using the newly created object whenever this is mentioned.
  4. It returns the newly created object, unless the constructor function returns a non-primitive value. In this case, that non-primitive value will be returned.
查看更多
疯言疯语
6楼-- · 2019-02-02 20:28

You are comparing object instances, which is not like a string comparison ('hello' === 'hello') Comparing objects in Javascript is actually comparing the memory addresses of the objects and will always return false because memory addresses are different for each object.

Compare the string values instead of the object instance - jsFiddle

( String('hello') === String('hello') ) // returns true due to comparing strings

Strictly comparing two objects - false not the same object

new String('hello') === new String('hello')

Strictly comparing two strings - true, same returned value and same returned type

String('hello') === String('hello')
查看更多
干净又极端
7楼-- · 2019-02-02 20:33

You are asking javascript to compare two different instances of the variable, not the string value that lives inside the variable.

So for example, lets say I have a piece of paper with the word "Hello World" written on it (Paper1) and my brother has a different piece of paper with the word "Hello World" written on it (Paper2).

When you say is Paper1 === Paper2 you will get false, beacuse no they are not the exact same piece of paper, even though the words written on the paper are the same.

If you where to say Paper1.toString() === Paper2 .toString() you would get true, beacuse we are comparing the words written on the paper, not the actual paper itself.

查看更多
登录 后发表回答