Skip to content

Instantly share code, notes, and snippets.

@proppy
Created June 28, 2010 19:42
Show Gist options
  • Save proppy/456257 to your computer and use it in GitHub Desktop.
Save proppy/456257 to your computer and use it in GitHub Desktop.
Object.prototype.setdefault = function(key, value) {
if (this[key] === undefined) {
this[key] = value;
}
return this[key];
};
test('Object.prototype.setdefault', function() {
var o = { b: 2 };
equals(1, o.setdefault('a', 1));
equals(2, o.setdefault('b', 1));
o.setdefault('c', []).push(3);
equals(3, o.c[0]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment