Zastąp porównanie równoważności w Javascript
Czy jest możliwe zastąpienie porównania równoważności w Javascript?
Najbliższe rozwiązanie, jakie udało mi się uzyskać, to zdefiniowanie funkcji valueOf i wywołanie valueOf z plusem przed obiektem.
To działa.
<code>equal(+x == +y, true); </code>
Ale to się nie udaje.
<code>equal(x == y, true, "why does this fail."); </code>
Oto moje przypadki testowe.
<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 tutaj:http://jsfiddle.net/tWyHg/5/