2

I am trying to play with objects alongside with Symbol.toPrimitive symbol in order to convert objects to primitives.

I used this code:

function UserEq(name) {
    this.name = name;
    this[Symbol.toPrimitive] = function (hint) {
        alert("hint is: " + hint);
        return 0;
    };
}
function objectEqualityCheck() {
    let user1 = new UserEq("John");
    let user2 = new UserEq("Rambo");

    if (user1 == user2) alert("Equal!");
}

And I expect two conversions resulting in alert Equal!, but that is not what's hapenning...

When used like user1 == 0 all works fine (conversion is to number though).

Why when two objects are compared, it does not work?

EDIT: it doesn't work with === as well.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • 2
    Because they are two different objects in two different spaces in memory. They are not the same object in the same allocated memory space, so by default object comparison, they are not equal – Taplar Sep 04 '19 at 18:21
  • @Taplar Oh, I thought that `==` operator would make implicit conversion. – Michał Turczyn Sep 04 '19 at 18:21
  • 2
    conversion occurs when you try to match two different type not when they are of same type – Code Maniac Sep 04 '19 at 18:21
  • check this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is – Arup Rakshit Sep 04 '19 at 18:22
  • I'm not familiar with how to implement the concept in javascript, but other languages like java based languages also have this same issue. They overcome it by implementing a method that defines how they are serialized or converted to a string, before simple equality will "just work". Otherwise, a custom `equals(secondObject)` can be written and that method can check that all the internals are equal. – Taplar Sep 04 '19 at 18:23
  • 1
    Possible duplicate of [How to determine equality for two JavaScript objects?](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) – VLAZ Sep 04 '19 at 18:24
  • in js two objects are never the same, checking for equality using `==` `===` would only compare the references which are sure to be different, so its always `false` unless you are comparing the same object. `==` , `===` equality does not check for the properties. – Anuja Sep 04 '19 at 18:27

1 Answers1

2

The first rule in the docs for == defines this

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

If Type(x) is the same as Type(y), then
    Return the result of performing Strict Equality Comparison x === y.

TC39


user1 == user2

Here you're comparing same type so there's no type conversion happens at all


ToPrimitive is called only under these conditions

  • If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
  • If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x) == y.
Code Maniac
  • 37,143
  • 5
  • 39
  • 60