Created
July 24, 2012 09:13
-
-
Save hkdobrev/3169016 to your computer and use it in GitHub Desktop.
isEqual for objects in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.isEqual = (a, b) -> | |
for p of a | |
return false if typeof b[p] is 'undefined' | |
return false if b[p] and not a[p] | |
t = typeof a[p] | |
return false if t is 'object' and not isEqual a[p], b[p] | |
return false if t is 'function' and ( typeof b[p] is 'undefined' or a[p].toString() isnt b[p].toString() ) | |
return false if a[p] isnt b[p] | |
for p of b | |
return false if typeof a[p] is 'undefined' | |
true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 1.3.3 | |
(function() { | |
window.isEqual = function(a, b) { | |
var p, t; | |
for (p in a) { | |
if (typeof b[p] === 'undefined') { | |
return false; | |
} | |
if (b[p] && !a[p]) { | |
return false; | |
} | |
t = typeof a[p]; | |
if (t === 'object' && !isEqual(a[p], b[p])) { | |
return false; | |
} | |
if (t === 'function' && (typeof b[p] === 'undefined' || a[p].toString() !== b[p].toString())) { | |
return false; | |
} | |
if (a[p] !== b[p]) { | |
return false; | |
} | |
} | |
for (p in b) { | |
if (typeof a[p] === 'undefined') { | |
return false; | |
} | |
} | |
return true; | |
}; | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment