Burada başka mixin uygulama görebilirsiniz:
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 });
biz sadece path
ve ek argümanlar geçirildi bir işlevdir katma durumun ele alınması gerektiğini olmanın fikir. Aksi takdirde, temel result()
uygulamasına geri dönersiniz.
_.myResult(object, 'test');
// → undefined
_.myResult(object, 'test', 15);
// → 15
_.myResult(object, 'cheese', 'wine');
// → "crumpets"
_.myResult(object, 'stuff');
// → "balderdash"
_.myResult(object, 'stuff', null, true);
// → "nonsense"
Dokümanlar, bu https://lodash.com/docs#result (eğer doğru anladıysam) işlemini gerçekleştirmenin herhangi bir yolunu göstermiyor gibi görünüyor, bu yüzden benim tahminim desteklenmiyor. Https://github.com/lodash/lodash/issues adresinden emin olmak isteyebilir veya ek bir özellik olarak talep edebilirsiniz. – Xotic750