Skip to content

Instantly share code, notes, and snippets.

@kentaromiura
Created February 25, 2014 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kentaromiura/9218529 to your computer and use it in GitHub Desktop.
Save kentaromiura/9218529 to your computer and use it in GitHub Desktop.
Object.get implementation
module.exports = function(object, path, defaultValue){
/*
if(arguments.length == 2){ // generic pattern
defaultValue = path;
path = object;
object = this;
}
*/
var result = defaultValue,
paths = path.split('.'),
part = '',
actual = object
while (part = paths.shift()){
if (actual !== null && typeof(actual) == 'object' && part in actual){
actual = actual[part]
} else {
actual = undefined
paths = []
}
}
if (actual !== undefined){
result = actual
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment