Created
December 20, 2011 13:30
-
-
Save geoffrasb/1501572 to your computer and use it in GitHub Desktop.
Object::toString() and Object::equal(obj)
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
Object.prototype.toString = function() { | |
var key, result, val; | |
result = ["{"]; | |
for (key in this) { | |
val = this[key]; | |
result = result.concat(key, ":", val, ","); | |
} | |
result.pop(); | |
return (result.concat("}")).join(""); | |
}; | |
/////////////////////////////////////////////////////////////////////////////////////////////// | |
/* coffeescript | |
Object::equal = (obj)-> | |
success = yes | |
try | |
((o1,o2)-> | |
(return null if (not o2[key]?) or (o2[key] isnt o1[key])) for key of o1 | |
return arguments.callee | |
)(obj,this)(this,obj) | |
catch e | |
success = no | |
return success | |
*/ | |
Object.prototype.equal = function(obj) { | |
var success; | |
success = true; | |
try { | |
(function(o1, o2) { | |
var key; | |
for (key in o1) { | |
if ((!(o2[key] != null)) || (o2[key] !== o1[key])) return null; | |
} | |
return arguments.callee; | |
})(obj, this)(this, obj); | |
} catch (e) { | |
success = false; | |
} | |
return success; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment