0

I read over a doc on equality comparison at MDN, but I'm interested in how Javascript performs strict equality checking.

There's also this specification: http://ecma-international.org/ecma-262/5.1/#sec-11.9.6, though, I don't really understand how it applies to two objects. The last statement is the key I think:

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

But how does Javascript check if they refer to the same object?

For example, given an object Kitten that has a name property, I create two kitties initialized with their names:

var kittenA = new Kitten("kitty A");
var kittenB = new Kitten("kitty B");

What does Javascript use to determine that that the following statement

kittenA === kittenB

Will return false?

MxLDevs
  • 19,048
  • 36
  • 123
  • 194
  • You might be interested in [How to explain object references in ECMAScript terms?](http://stackoverflow.com/q/23554770/1048572). I don't think there's much else behind it. – Bergi Apr 28 '16 at 16:31
  • Your title question sounds a bit like a duplicate [Difference between == and === in JavaScript](http://stackoverflow.com/q/523643/1048572). You should edit it to make clear that your question is limited to objects. – Bergi Apr 28 '16 at 16:35
  • It's checking whether the references are identical (i.e. both `kittenA` and `kittenB` point to the same underlying object). In this case the references point to different objects which is why it returns false. – grovesNL Apr 28 '16 at 16:38
  • @Bergi I have updated the title. I also looked over the answer, but I'm not sure which part would help me (or someone) understand how javascript checks an object's reference. – MxLDevs Apr 28 '16 at 16:39
  • @KarolyHorvath someone asked me the same question and I couldn't provide an adequate answer. – MxLDevs Apr 28 '16 at 16:40
  • @MxyL: The answer is [object identity](https://en.wikipedia.org/wiki/Identity_(object-oriented_programming)). Each call to `new Kitten` creates a new object, and two objects are not the same. Or are you asking how objects are implemented (and compared) in a javascript engine? – Bergi Apr 28 '16 at 16:40
  • @Bergi More or less how a "reference" to an object is obtained by the JS engine, so that it can compare whether they "refer" to the same object or not. Implementation may vary, but I'd be interested to know if such information is available publicly. – MxLDevs Apr 28 '16 at 16:47
  • 1
    @MxyL: Both V8 and Gecko are open source, so yes you can look it up if you're interested. An object reference is typically just a (possibly tagged) pointer to some block of memory that contains data of and about the object. – Bergi Apr 28 '16 at 16:51
  • @Bergi thank you. I will look further into the specifics. – MxLDevs Apr 28 '16 at 17:00

1 Answers1

3

kittenA and kittenB are both of type object so case 7 applies:

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

How does the engine know they are two references to the same object? How a reference is implemented exactly will vary per engine. It might just be a number pointing to a memory location or an index in a list (of all objects).

Halcyon
  • 57,230
  • 10
  • 89
  • 128