Skip to content

Instantly share code, notes, and snippets.

@qzm
Created April 17, 2017 06:56
Show Gist options
  • Save qzm/2ddac17dd22fde16b09cadc894e9dc06 to your computer and use it in GitHub Desktop.
Save qzm/2ddac17dd22fde16b09cadc894e9dc06 to your computer and use it in GitHub Desktop.
判断两个值是否相等
/**
* 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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment