2

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??

Terry
  • 127
  • 1
  • 11
  • 1
    possible duplicate of [In Javascript, why is \[1, 2\] == \[1, 2\] or ({a : 1}) == ({a : 1}) false?](http://stackoverflow.com/questions/7713794/in-javascript-why-is-1-2-1-2-or-a-1-a-1-false) – Felix Kling Jan 02 '12 at 10:50
  • "Understanding pointers is not a skill, it's an aptitude". "p" and "o" are different pointers, both protos are the same one. – georg Jan 02 '12 at 13:26
  • Even if `p` and `o` are different pointers, `===` can still be true. The important thing is whether the two pointers point to the same object. – Mathias Schwarz Jan 03 '12 at 10:08

7 Answers7

6

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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • 1
    what is the exact same object? – Terry Jan 02 '12 at 10:50
  • 2
    @caizZZz: Consider `var o = {}; var p = o;` `p` and `o` refer to the same object. If I modify a property of `p`, `o` is changed as well because they both point to the same instance. – Felix Kling Jan 02 '12 at 10:53
  • but why `o.__proto__===p.__proto__` // true – Terry Jan 02 '12 at 11:05
  • 1
    Because the Object prototype isn't newly generated upon instantiation. `__proto__` still refers to Object's prototype in both cases. – goto-bus-stop Jan 02 '12 at 11:19
  • 1
    @caizZZz: While it might be surprising that both of `__proto__` properties point to the same object, it should not be surprising that two *different* objects can refer to the same other object. Consider: `var foo = {}, bar = {x: foo, y: 42}, baz = {x: foo};`. Clearly `bar` and `baz` cannot be the same object but `bar.x === baz.x` is `true`. – Felix Kling Jan 02 '12 at 11:28
6

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 {}.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
3

JavaScript strict comparison for objects tests whether two expressions refer to the same objects (and so does the normal equals operator).

You create the first object using object literal {} which creates a new object with no properties.

You create the second object by calling Object constructor as a function. According to section 15.2.1.1 of ECMAScript Language Specification this also creates a new object just as if new Object() was used.

Thus you create two objects, store their references under p and o and check whether p and o refer to the same object. They don't.

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
1

Every time you create an object, the result has its own, distinct identity. So even though they are both "empty", they are not the same thing. Hence the === comparison yields false.

Xion
  • 22,400
  • 10
  • 55
  • 79
1

Using the === , the result will show if items on both side is the "Same Instance"

If you like to comparing two item are same type, you should use:

var o1 = {};
var o2 = new Object();

alert( typeof(o1) === typeof(o2));

and if you like to tell if the two object is considered equal (in properties and values), you should try underscore.js library and use the isEqual function.

taiansu
  • 2,735
  • 2
  • 22
  • 29
0

Is this homework?

In that case, I will only give you some hints: - Think about what the first two lines do. Do o and p refer to the same object after those two lines? - Look up exactly what === does. Does it compare two objects to see if their structure are the same? Or does it compare the objects based on their identity?

Mathias Schwarz
  • 7,099
  • 23
  • 28
0

Object is an unordered collection of properties each of which contains a primitive value, object, or function. So, each object has properties and prototypes and there's no any sense to compare the one.

Alexander Abashkin
  • 1,187
  • 4
  • 13
  • 18