Skip to content

Instantly share code, notes, and snippets.

@Thermatix
Forked from sposmen/ObjectFetch.js
Last active October 7, 2016 15:09
Show Gist options
  • Save Thermatix/2b49a80ed90168ef98317ea5179df6e9 to your computer and use it in GitHub Desktop.
Save Thermatix/2b49a80ed90168ef98317ea5179df6e9 to your computer and use it in GitHub Desktop.
Equivalent to Ruby hash fetch in Javascript
Object.prototype.fetch = function (key, value,callback) {
return this.hasOwnProperty(key) ? this[key] : (value ? value : (callback ? callback.call : (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