function reduceAncestors(person, f, defaultValue) {
function valueFor(person) {
if (person == null)
return defaultValue;
else
return f(person, valueFor(byName[person.mother]),
valueFor(byName[person.father]));
}
return valueFor(person);
}
function sharedDNA(person, fromMother, fromFather) {
if (person.name == "Pauwels van Haverbeke")
return 1;
else
return (fromMother + fromFather) / 2;
}
var ph = byName["Philibert Haverbeke"];
console.log(reduceAncestors(ph, sharedDNA, 0) / 4);
// → 0.00049
我在理解 Eloquent Javascript 的这个例子时遇到了很多困难。这是高阶函数章节(第 5 章)末尾的示例,标题为“Great-Great-Great...”,我看不出 fromMother 和 fromFather 从哪里获取它们的值。我认为他们指的是 valueFor(byName[person.mother] 和 valueFor(byName[person.father]),但我不知道“valueFor”函数如何或何时从数据中检索实际数值(请注意,我显然没有在这篇文章中包含数据,也没有包含 byName 函数)。提前感谢您的帮助!
reduceAncestors(ph, sharedDNA, 0) / 4)
通过sharedDNA
函数作为第二个参数。
它被分配给f
here:
function reduceAncestors(person, f, defaultValue)
f
在这里被调用:
f(person, valueFor(byName[person.mother]), valueFor(byName[person.father]));
…您可以在其中看到分配给的第二个和第三个参数fromMother
and fromFather
:
function sharedDNA(person, fromMother, fromFather) {
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)