Ecco un'altra implementazione mixin:
function myResult(obj, path, defaultValue) {
// Find any arguments beyond what's normally
// passed to result().
var args = _.drop(arguments, 3);
// We need to know upfront whether or not this
// is a function we're dealing with.
var isFunc = _.isFunction(_.get(obj, path));
// If this is a function, and there's arguments
// to apply, then use spread() and bindKey() to
// return the result of calling the method with arguments.
// Otherwise, it's just a plain call to result().
if (isFunc && args.length) {
return _.spread(_.bindKey(obj, path))(args);
} else {
return _.result(obj, path, defaultValue);
}
}
_.mixin({ myResult: myResult });
L'idea è che abbiamo solo bisogno di gestire il caso in cui aggiunto path
è una funzione sono stati passati e argomenti aggiuntivi. In caso contrario, torniamo all'implementazione di base result()
.
_.myResult(object, 'test');
// → undefined
_.myResult(object, 'test', 15);
// → 15
_.myResult(object, 'cheese', 'wine');
// → "crumpets"
_.myResult(object, 'stuff');
// → "balderdash"
_.myResult(object, 'stuff', null, true);
// → "nonsense"
fonte
2015-12-30 16:45:05
I documenti non sembrano mostrare alcun modo per realizzare questo https://lodash.com/docs#result (se ho capito bene) quindi la mia ipotesi è che questo non è supportato. Potresti chiedere qui https://github.com/lodash/lodash/issues per essere sicuro, o addirittura richiederlo come funzionalità aggiuntiva. – Xotic750