Eloquent Javascript:高阶函数示例 [英] Eloquent Javascript: Higher Order Functions example

查看:76
本文介绍了Eloquent Javascript:高阶函数示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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函数。)提前感谢任何帮助!

I am having a lot of trouble understanding this example from Eloquent Javascript. This is the example at the end of the Higher Order Functions Chapter (chpt 5), under the heading "Great-Great-Great..." I don't see where fromMother and fromFather are getting their values. I think that they are referring to valueFor(byName[person.mother] and valueFor(byName[person.father]), but I don't see how or when the "valueFor" functions ever retrieve an actual number value from the data (note that I obviously didn't include the data in this post, nor did I include the byName function). Thanks in advance for any help!

推荐答案

reduceAncestors(ph,sharedDNA,0)/ 4) sharedDNA 函数作为第二个参数传递。

reduceAncestors(ph, sharedDNA, 0) / 4) passes the sharedDNA function as the second argument.

它被分配到 f 这里:

function reduceAncestors(person, f, defaultValue)

<$在这里调用c $ c> f :

f(person, valueFor(byName[person.mother]), valueFor(byName[person.father]));

...在那里你可以看到从母亲那里分配给的第二个和第三个参数 fromFather

… where you can see the second and third arguments which get assigned to fromMother and fromFather:

function sharedDNA(person, fromMother, fromFather) {

这篇关于Eloquent Javascript:高阶函数示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆