qunit

Override the Equivalence Comparison in Javascript

怎甘沉沦 提交于 2019-11-26 23:03:01
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 () {

Override the Equivalence Comparison in Javascript

一世执手 提交于 2019-11-26 08:34:44
问题 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