zoukankan      html  css  js  c++  java
  • 两个对象深度比较,借鉴,记录

        function deepCompare(x, y) {
            var i, l, leftChain, rightChain;
            function compare2Objects(x, y) {
                var p;
                if (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') {
                    return true;
                }
                if (x === y) {
                    return true;
                }
                if ((typeof x === 'function' && typeof y === 'function') ||
                    (x instanceof Date && y instanceof Date) ||
                    (x instanceof RegExp && y instanceof RegExp) ||
                    (x instanceof String && y instanceof String) ||
                    (x instanceof Number && y instanceof Number)) {
                    return x.toString() === y.toString();
                }
                if (!(x instanceof Object && y instanceof Object)) {
                    return false;
                }
                if (x.isPrototypeOf(y) || y.isPrototypeOf(x)) {
                    return false;
                }
     
                if (x.constructor !== y.constructor) {
                    return false;
                }
     
                if (x.prototype !== y.prototype) {
                    return false;
                }
     
                if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) {
                    return false;
                }
     
                for (p in y) {
                    if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
                        return false;
                    } else if (typeof y[p] !== typeof x[p]) {
                        return false;
                    }
                }
     
                for (p in x) {
                    if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
                        return false;
                    } else if (typeof y[p] !== typeof x[p]) {
                        return false;
                    }
     
                    switch (typeof(x[p])) {
                        case 'object':
                        case 'function':
     
                            leftChain.push(x);
                            rightChain.push(y);
     
                            if (!compare2Objects(x[p], y[p])) {
                                return false;
                            }
     
                            leftChain.pop();
                            rightChain.pop();
                            break;
     
                        default:
                            if (x[p] !== y[p]) {
                                return false;
                            }
                            break;
                    }
                }
     
                return true;
            }
            
            if (arguments.length < 1) { // 这里判断如果没有传值直接返回true
                return true;
            }
     
            for (i = 1, l = arguments.length; i < l; i++) {
                leftChain = []; 
                rightChain = [];
     
                if (!compare2Objects(arguments[0], arguments[i])) {
                    return false;
                }
            }
     
            return true;
        }
    小凤凰newObject
  • 相关阅读:
    bzoj1101 [POI2007]Zap
    bzoj2648/2716 kdtree
    bzoj2850巧克力王国
    【bzoj1193】[HNOI2006]马步距离
    bzoj 4401 块的计数 思想+模拟+贪心
    【bzoj2751】[HAOI2012]容易题(easy) 数论,简单题
    Ubuntu分区小知识与分区方案
    Ubuntu16.04安装x11VNC远程桌面
    Ubuntu用户权限管理(chown, chmod)
    Ubuntu新建用户组
  • 原文地址:https://www.cnblogs.com/xiaofenghuang/p/13936515.html
Copyright © 2011-2022 走看看