Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IN javascript,why {}!==Object()?

Given

    var o = {};
    var p = new Object();

    p === o; //false

    o.__proto__===p.__proto__  // true

why is this false?

please tell me the immediate reason to return false??

like image 510
Terry Avatar asked Sep 22 '26 04:09

Terry


2 Answers

The === for objects is defined as:

11.9.6 The Strict Equality Comparison Algorithm

The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:

...

7. Return true if x and y refer to the same object. Otherwise, return false.

In this case, although both are empty objects, they are created separately and hence do not refer to the same object.

As a side note, both constructions do the same thing; but it is common practice to use {}.

like image 188
pimvdb Avatar answered Sep 24 '26 21:09

pimvdb


The two objects contain the same thing (i.e. nothing) but they are not the same object.

Javascript's object equality test requires that the two parameters refer to the exact same object.

like image 44
Alnitak Avatar answered Sep 24 '26 21:09

Alnitak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!