Skip to content

Instantly share code, notes, and snippets.

@sposmen
Created March 2, 2016 15:25
Show Gist options
  • Save sposmen/8834652073bef03fd661 to your computer and use it in GitHub Desktop.
Save sposmen/8834652073bef03fd661 to your computer and use it in GitHub Desktop.
Equivalent to Ruby hash fetch in Javascript
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