Skip to content

Instantly share code, notes, and snippets.

@geoffrasb
Created December 20, 2011 13:30
Show Gist options
  • Save geoffrasb/1501572 to your computer and use it in GitHub Desktop.
Save geoffrasb/1501572 to your computer and use it in GitHub Desktop.
Object::toString() and Object::equal(obj)
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