Created
April 11, 2012 18:52
-
-
Save lu1s/2361359 to your computer and use it in GitHub Desktop.
javascript object equality
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
// y and x can be whatever you look for equality | |
function objeq(y,x) | |
{ | |
var p; | |
for(p in y) { | |
if(typeof(x[p])=='undefined') {return false;} | |
} | |
for(p in y) { | |
if (y[p]) { | |
switch(typeof(y[p])) { | |
case 'object': | |
if (!objeq(y[p],x[p])) { return false; } break; | |
case 'function': | |
if (typeof(x[p])=='undefined' || | |
(p != 'equals' && y[p].toString() != x[p].toString())) | |
return false; | |
break; | |
default: | |
if (y[p] != x[p]) { return false; } | |
} | |
} else { | |
if (x[p]) | |
return false; | |
} | |
} | |
for(p in x) { | |
if(typeof(y[p])=='undefined') {return false;} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment