比较两个对象是否完全相等
deepVerificationObject(newObj, oldObj) { let i, l, leftChain, rightChain; function compareObjectDifferences(newObj, oldObj) { let p;
if (isNaN(newObj) && isNaN(oldObj) && typeof newObj === 'number' && typeof oldObj === 'number') { return true; } if (newObj === oldObj) { return true; } if ((typeof newObj === 'function' && typeof oldObj === 'function') || (newObj instanceof Date && oldObj instanceof Date) || (newObj instanceof RegExp && oldObj instanceof RegExp) || (newObj instanceof String && oldObj instanceof String) || (newObj instanceof Number && oldObj instanceof Number)) { return newObj.toString() === oldObj.toString(); }
if (!(newObj instanceof Object && oldObj instanceof Object)) { return false; } if (newObj.isPrototypeOf(oldObj) || oldObj.isPrototypeOf(newObj)) { return false; } if (newObj.constructor !== oldObj.constructor) { return false; } if (newObj.prototype !== oldObj.prototype) { return false; } if (leftChain.indexOf(newObj) > -1 || rightChain.indexOf(oldObj) > -1) { return false; } for (p in oldObj) { if (oldObj.hasOwnProperty(p) !== newObj.hasOwnProperty(p)) { return false; } else if (typeof oldObj[p] !== typeof newObj[p]) { return false; } } for (p in newObj) { if (oldObj.hasOwnProperty(p) !== newObj.hasOwnProperty(p)) { return false; } else if (typeof oldObj[p] !== typeof newObj[p]) { return false; } switch (typeof(newObj[p])) { case 'object': case 'function': leftChain.push(newObj); rightChain.push(oldObj); if (!compareObjectDifferences(newObj[p], oldObj[p])) { return false; } leftChain.pop(); rightChain.pop(); break; default: if (newObj[p] !== oldObj[p]) { return false; } break; } } return true; } if (arguments.length < 1) { return true; } for (i = 1, l = arguments.length; i < l; i++) { leftChain = []; rightChain = []; if (!compareObjectDifferences(arguments[0], arguments[i])) { return false; } } return true; }