-1

folks.

I was working on implementations to a (stucked ES5) enterprise compilator and I got some nice implementations like:

Object.prototype.isString = function () { return typeof this[0] === 'string' }

'asd'.isString (); // True
(123).isString (); // False
new Date ().isString (); // False

and

Date.prototype.getArray = function () {
    var mm = this.getMonth () + 1;
    var dd = this.getDate ();

    var arr = new Array ();
    arr.push (this.getFullYear ());
    arr.push ((mm > 9 ? '' : '0') + mm);
    arr.push ((dd > 9 ? '' : '0') + dd);

    return arr;
};

var date = new Date ();
date.getArray ()[0]; // 2020 (Year)
date.getArray ()[1]; // 10 (Month)
date.getArray ()[2]; // 21 (Day)

My question is: I'm trying to implement the under snnipet but I'm not getting the equality comparation result. Someone just had made it?

Object.prototype.equals = function (toCompare) { return this === toCompare; }
W . S.
  • 19
  • 6

1 Answers1

0

For who was interested, I found the answer on a @Jevgeni Kiski comment, thanks a lot for @Gillall

Object.prototype.equals = function(x){
    for (var p in this) {
        if(typeof(this[p]) !== typeof(x[p])) return false;
        if((this[p]===null) !== (x[p]===null)) return false;
        switch (typeof(this[p])) {
            case 'undefined':
                if (typeof(x[p]) != 'undefined') return false;
                break;
            case 'object':
                if(this[p]!==null && x[p]!==null && (this[p].constructor.toString() !== x[p].constructor.toString() || !this[p].equals(x[p]))) return false;
                break;
            case 'function':
                if (p != 'equals' && this[p].toString() != x[p].toString()) return false;
                break;
            default:
                if (this[p] !== x[p]) return false;
        }
    }
    return true;
}
W . S.
  • 19
  • 6