I have created new objects with Dojo.declare. How to overload operator == for objects ?
            Asked
            
        
        
            Active
            
        
            Viewed 4,551 times
        
    7
            
            
        - 
                    Possible duplicate of [Overloading Arithmetic Operators in JavaScript?](http://stackoverflow.com/questions/1634341/overloading-arithmetic-operators-in-javascript) – Ciro Santilli OurBigBook.com Dec 18 '15 at 00:01
 - 
                    Here: https://stackoverflow.com/q/10539938/632951 – Pacerier Sep 19 '17 at 00:12
 
3 Answers
9
            You can't overload ==, but == has an implicit .toString() call, so whatever .toString() returns will allow you to effectively overload == (kinda):
function foo(){}
foo.prototype.toString = function(){ return 42; }
var x = new foo();
x == 42; // true
As for how to do this in Dojo, I don't use Dojo, sorry, but the gist is that you get a reference to whatever object is creates and add thatObject.prototype.toString as in my example.
- 
                    5For an object and a primitive, maybe. Though `valueOf` is what you would want to override. But not for two objects. `new String('x') != new String('x')` despite the fact that both objects' `toString` methods return the same value. – Mike Samuel Apr 11 '11 at 15:53
 - 
                    1@Mike - meh. You're right. Object equality doesn't apply because there is only a check to the memory address... – Apr 11 '11 at 16:28
 - 
                    1even for object and primitive comparison the priority will be given to `valueOf`implementation. – Maksim Vi. Aug 07 '15 at 23:13
 
3
            
            
        You can't in Javascript/ECMAscript. You can overload operators in ExtendScript from Adobe. See this example. Also see this blog entry (pro), or this (contra).
        Lupus Ossorum
        
- 450
 - 3
 - 16
 
        KooiInc
        
- 119,216
 - 31
 - 141
 - 177
 
- 
                    
 - 
                    @Joachim Sauer: wtf? That's what I said this answer! Adding some links to show there's discussion on operator overloading for ECMAscript. – KooiInc Apr 11 '11 at 09:27