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
  • 相关阅读:
    洛谷P2742 【模板】二维凸包
    计算几何笔记
    洛谷P1251 餐巾计划问题(最小费用最大流)
    洛谷P2762 太空飞行计划问题(最大权闭合图)
    洛谷P2764 最小路径覆盖问题(二分图)
    [置顶] Guava学习之ArrayListMultimap
    sphinx coreseek SetSortMode(SPH_SORT_ATTR_ASC, '') 对float 排序设置bug
    magento 修改 paypal order product name
    硬盘“坏了”怎么办
    能够兼容ViewPager的ScrollView
  • 原文地址:https://www.cnblogs.com/xiaofenghuang/p/13936515.html
Copyright © 2011-2022 走看看