I want to define a JavaScript class, Foo.
Foo = function(value){
this.value = value;
};
I will create "instances" of my Foo:
foo1 = new Foo(1);
foo2 = new Foo(1);
and I want my instances of Foo to be comparable with each other using the standard == equality operator:
foo1 == foo2; // this should be true
I can not find a way to do this. I thought I was on to something with the valueOf()
function, but this is only useful when one side of the comparison is a primitive, not as above where both are of type object.
Have I missed something really simple akin to Ruby's
def ==(obj); end
I want to define a JavaScript class, Foo.
Foo = function(value){
this.value = value;
};
I will create "instances" of my Foo:
foo1 = new Foo(1);
foo2 = new Foo(1);
and I want my instances of Foo to be comparable with each other using the standard == equality operator:
foo1 == foo2; // this should be true
I can not find a way to do this. I thought I was on to something with the valueOf()
function, but this is only useful when one side of the comparison is a primitive, not as above where both are of type object.
Have I missed something really simple akin to Ruby's
def ==(obj); end
Share
Improve this question
edited Jan 13, 2009 at 23:48
Peter Hilton
17.3k6 gold badges51 silver badges76 bronze badges
asked Jan 13, 2009 at 22:38
Chris FarmiloeChris Farmiloe
14.2k5 gold badges50 silver badges57 bronze badges
2
- have you tried "foo1 === foo2" ? – Joel Coehoorn Commented Jan 13, 2009 at 22:56
- Here: stackoverflow.com/q/10539938/632951 – Pacerier Commented Sep 19, 2017 at 0:15
4 Answers
Reset to default 9JavaScript does not have operator overloading. See this discussion covering your question.
Not in a clean way...
Foo = function (value){
this.value = value;
};
Override the toString function of your own object:
Foo.prototype.toString = function( ){ return this.value.toString(); }
Creating two test objects:
foo1 = new Foo(1);
foo2 = new Foo(1);
If the values of your objects is strings or numbers, you can have the javascript engine to convert your objects to a string by adding them to an empty string:
alert( ""+foo1 === ""+foo2 ); //Works for strings and numbers
The same thing but cleaner:
alert( foo1.toString() === foo2.toString() ); //Works for strings and numbers
If the values of your objects is numeric only, you can use the unary + operator to convert the object to a number:
alert( +foo1 === +foo2 ); //Works for numbers
However, I recommend you to both define the toString as above and also an equals:
Foo.prototype.equals=function(b){return this.toString() === b.toString();}
And then call it like this:
alert ( foo1.equals(foo2) );
Since you now have defined toString you can:
alert(foo1); // Alerts the value of foo1 instead of "[Object object]"
alert("foo1: " + foo1); // Alerts "foo1: 1". Useful when debugging.
JavaScript is in this regard like its namesake Java: No operator overloading. Use a custom method like equals()
instead.
I don't think this is possible using javascript. You have to write your own function e.g. isEqual()