Is it possible to override the equivalence comparison in Javascript?
The closest I have gotten to a solution is by defining the valueOf function and invoking valueOf with a plus in front of the object.
This works.
equal(+x == +y, true);
But this fails.
equal(x == y, true, "why does this fail.");
Here are my test cases.
var Obj = function (val) { this.value = val; }; Obj.prototype.toString = function () { return this.value; }; Obj.prototype.valueOf = function () { return this.value; }; var x = new Obj(42); var y = new Obj(42); var z = new Obj(10); test("Comparing custom objects", function () { equal(x >= y, true); equal(x <= y, true); equal(x >= z, true); equal(y >= z, true); equal(x.toString(), y.toString()); equal(+x == +y, true); equal(x == y, true, "why does this fails."); });
Demo here: http://jsfiddle.net/tWyHg/5/
Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...
The strict equality operator ( === ) behaves identically to the abstract equality operator ( == ) except no type conversion is done, and the types must be the same to be considered equal. The == operator will compare for equality after doing any necessary type conversions.
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.
= is used for assigning values to a variable in JavaScript. == is used for comparison between two variables irrespective of the datatype of variable. === is used for comparision between two variables but this will check strict type, which means it will check datatype and compare two values.
That is because the ==
operator doesn't compare only primitives, therefore doesn't call the valueOf()
function. Other operators you used do work with primitives only. I'm afraid you cannot achieve such thing in Javascript. See http://www.2ality.com/2011/12/fake-operator-overloading.html for some more details.
Piggybacking on @Corkscreewe:
This is because you are dealing with Objects and the equivalency operators are only going to compare whether two variables reference the same Object, not whether the two Objects are somehow equal.
One solution is to use "+" in front of the variables and define a valueOf method for the Objects. This calls the valueOf method on each object to "cast" its value to a Number. You have already found this, but understandably do not seem very satisfied with it.
A more expressive solution might be to define an equals function for your Objects. Using your examples above:
Obj.prototype.equals = function (o) { return this.valueOf() === o.valueOf(); }; var x = new Obj(42); var y = new Obj(42); var z = new Obj(10); x.equals(y); // true x.equals(z); // false
I know this doesn't do exactly what you want (redefine the equivalency operators themselves), but hopefully it will get you a little closer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With