Skip to content

Instantly share code, notes, and snippets.

@jaredks
Created December 28, 2014 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredks/76c8e8e9b0a646d4c608 to your computer and use it in GitHub Desktop.
Save jaredks/76c8e8e9b0a646d4c608 to your computer and use it in GitHub Desktop.
Python dict methods for JS objects
Object.defineProperty(Object.prototype, "setdefault", {
value: function(key, value) {
if (!this.hasOwnProperty(key)) {
if (typeof value === 'undefined') value = null;
this[key] = value;
return value;
}
return this[key];
}
});
Object.defineProperty(Object.prototype, "pop", {
value: function(key, value) {
if (!this.hasOwnProperty(key)) {
if (typeof value !== 'undefined') return value;
return this[key];
}
value = this[key];
delete this[key];
return value;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment