はここで別のミックスインの実装です:
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 });
アイデアは、我々は唯一のpath
がと追加の引数が渡された関数である追加ケースを処理する必要があること。それ以外の場合は、基本的な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"
ドキュメントがショーに(私が正しく理解している場合)、このhttps://lodash.com/docs#resultを達成するためにどのような方法をいないようですので、私の推測では、これはサポートされていないことです。ここではhttps://github.com/lodash/lodash/issuesに特定のことを尋ねたり、追加の機能としてリクエストすることもできます。 – Xotic750