Anular la comparación de equivalencia en Javascript
¿Es posible anular la comparación de equivalencia en Javascript?
Lo más cercano que he llegado a una solución es definir la función valueOf e invocar valueOf con un signo más delante del objeto.
Esto funciona.
<code>equal(+x == +y, true); </code>
Pero esto falla.
<code>equal(x == y, true, "why does this fail."); </code>
Aquí están mis casos de prueba.
<code>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."); }); </code>
Demo aquí:http://jsfiddle.net/tWyHg/5/