Last active
December 16, 2015 05:59
-
-
Save vpalos/5388068 to your computer and use it in GitHub Desktop.
Universal field getter for JavaScript 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
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Long comment:
https://gist.github.com/CristianTincu/a4e137bb5cae3f11fadd
FWIW: Posting comments on your personal blog returns 403. ☺