Created
March 2, 2016 15:25
-
-
Save sposmen/8834652073bef03fd661 to your computer and use it in GitHub Desktop.
Equivalent to Ruby hash fetch in Javascript
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.prototype.fetch = function (key, value) { | |
return this.hasOwnProperty(key) ? this[key] : (value ? value : (function () { | |
throw new Error('key not found') | |
})()); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment