Update
The answer from @stasovlas is the right approach.
Quick and easy
The droid you were looking for:
_.get(this, 'doSomething', _.noop)()
It is quite self-documenting, functionally equivalent:
if (this.doSomething) { this.doSomething(); } else { _.noop(); }
Better for reuse
If you want to reuse this on your code base, you can use mixin to make it more readable and compressed, and bake it directly in lodash:
var runIfFunction = function(value) { if (_.isFunction(value)) { return value(); } }; _.mixin({runIfFunction: runIfFunction});
What allows you to do this:
_.runIfFunction(this.doSomething);
source share