Override the Equivalence Comparison in Javascript

后端 未结 5 479
一整个雨季
一整个雨季 2020-11-28 15:55

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

5条回答
  •  天涯浪人
    2020-11-28 16:06

    you can use the ES6 Object.is() function to check the property of object.

    Object.prototype.equals = function(obj)
    {
        if(typeof obj != "Object")
            return false;
        for(var property in this)
        {
            if(!Object.is(obj[property], this[property]))
                return false;
        }
        return true;
    }
    

提交回复
热议问题