Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make comparison of objects `a == b` to be true? [duplicate]

Here is one of the questions in JavaScript online-test before job interview:

function F(){};

var a = new F();
var b = new F();

Q: How to make comparison a == b to be true? (e.g. console.log(a == b) // true)

I answered that it's impossible because a and b are two different instances of F and equal comparison in JS in case of non-primitives compares reference.

But some time ago I've read article "Fake operator overloading in JavaScript" by Axel Rauschmayer: http://www.2ality.com/2011/12/fake-operator-overloading.html — and I wonder if there is a hack to fake operator overload in comparison of objects?

like image 533
jsguff Avatar asked Jun 30 '13 11:06

jsguff


People also ask

Can you compare objects with ==?

Comparing Objects ¶ When using the comparison operator ( == ), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values (values are compared with == ), and are instances of the same class.

Can you use == for objects in Java?

equals() method in Java. Both equals() method and the == operator are used to compare two objects in Java. == is an operator and equals() is method. But == operator compares reference or memory location of objects in a heap, whether they point to the same location or not.

What is the difference between == and === while comparing objects?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

How do you compare equality of objects?

The behavior for performing loose equality using == is as follows: If the operands have the same type, they are compared as follows: Object: return true only if both operands reference the same object. String: return true only if both operands have the same characters in the same order.


1 Answers

It really depends on what they mean by "How to make comparison a == b to be true?"

If you're allowed to change the constructor, then you could make your constructor a singleton:

function F(){
    if (!F.instance) {
        F.instance = this;
    } else {
        return F.instance;
    }
};
var a = new F();
var b = new F();
if (a === b) {
    //they are the same
}

If they want you to keep everything as it is but have a comparision that contains a == b then you could write the following:

if ("" + a == b) {
}

If they want to know methods of determine whether the two objects are instances of the same constructor function, then you could compare the constructor property or the __proto__ property:

if (a.constructor === b.constructor) {
}

if (a.__proto__ === b.__proto__) {
}

If they want to know methods of dermine whether these two objects have the same properties, you can either compare their JSON string:

if (JSON.stringify(a) === JSON.stringify(b)) {
}

or you write a function that recursively compares all the properties in both objects (deep comparision).

And the most simple answer to the question "How to make comparison a == b to be true?":

var a = new F();
var b = new F();

b = a;

if (a === b) {
    //surprise!!!
}
like image 200
basilikum Avatar answered Sep 23 '22 18:09

basilikum