Skip to content

Instantly share code, notes, and snippets.

@vpalos
Last active December 16, 2015 05:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpalos/5388068 to your computer and use it in GitHub Desktop.
Save vpalos/5388068 to your computer and use it in GitHub Desktop.
Universal field getter for JavaScript objects.
/**
* Universal field getter method for JavaScript objects.
* @param {Object} _path The field path inside `this`.
* @param {...} _default The default value to be returns if field is not found.
* @return {...} Returns the found field value else `_default` else `undefined`.
*/
Object.prototype._ = Object.prototype._ || function(_path, _default) {
var value = _path.split('.').reduce(
function(hash, field) {
return hash && hash[field]
},
this
);
return typeof(value) === 'undefined' ? _default : value;
}
@c7tincu
Copy link

c7tincu commented Apr 19, 2013

Long comment:
https://gist.github.com/CristianTincu/a4e137bb5cae3f11fadd

FWIW: Posting comments on your personal blog returns 403. ☺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment