qzm
4/17/2017 - 6:56 AM

判断两个值是否相等

判断两个值是否相等

/**
 * Check if two values are loosely equal - that is,
 * if they are plain objects, do they have the same shape?
 */
function looseEqual(a, b) {
    var isObjectA = Object.prototype.toString.call(a) === '[object Object]';
    var isObjectB = Object.prototype.toString.call(b) === '[object Object]';
    if (isObjectA && isObjectB) {
        return JSON.stringify(a) === JSON.stringify(b)
    } else if (!isObjectA && !isObjectB) {
        return String(a) === String(b)
    } else {
        return false
    }
}