Created
December 28, 2014 08:41
-
-
Save jaredks/76c8e8e9b0a646d4c608 to your computer and use it in GitHub Desktop.
Python dict methods for JS objects
This file contains 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.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