For example,
check if isCourage()
method exist in such object.
puppy {
...
goldenRetriever {
...
hasCourage(){ ... }
}
}
Lodash can do the trick but i'm not sure which method should i use.
With lodash you have a few functions that might be helpful to you:
You can use _.isFunction
with _.get
:
let obj = {
foo:{
bar:{
fooBar: function(b) { return b }
}
}
}
console.log(_.isFunction(_.get(obj,'foo.bar.fooBar')))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
You can also use _.result
to get the value right away and not care
really if it is or not a function. _.result
would walk the path executing anything that is a function and return the final value:
let obj = {
foo:{
bar:{
fooBar: function(b='yay') { return b }
}
}
}
console.log(_.result(obj, 'foo.bar.fooBar'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>