...
Foo = function (value){
this.value = value;
};
toString :
Foo.prototype.toString = function( ){ return this.value.toString(); }
:
foo1 = new Foo(1);
foo2 = new Foo(1);
, javascript , :
alert( ""+foo1 === ""+foo2 ); //Works for strings and numbers
, :
alert( foo1.toString() === foo2.toString() ); //Works for strings and numbers
, unary + :
alert( +foo1 === +foo2 ); //Works for numbers
toString, , :
Foo.prototype.equals=function(b){return this.toString() === b.toString();}
:
alert ( foo1.equals(foo2) );
toString, :
alert(foo1); // Alerts the value of foo1 instead of "[Object object]"
alert("foo1: " + foo1); // Alerts "foo1: 1". Useful when debugging.