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?