Javascript - 将函数传递给未定义的函数

时间:2022-12-05 16:06:53

Trying to solve "Historical Life Expectancy" problem on http://eloquentjavascript.net/05_higher_order.html.

试图在http://eloquentjavascript.net/05_higher_order.html上解决“历史人生预期”问题。

The solution from http://eloquentjavascript.net/code/#5.3 looks like:

来自http://eloquentjavascript.net/code/#5.3的解决方案如下所示:

function average(array) {
  function plus(a, b) { return a + b; }
  return array.reduce(plus) / array.length;
}

function groupBy(array, groupOf) {
  var groups = {};
  array.forEach(function(element) {
    if (groupOf(element) in groups)
      groups[groupOf(element)].push(element);
    else
      groups[groupOf(element)] = [element];
  });
  return groups;
}

var byCentury = groupBy(ancestry, function(person) {
  return Math.ceil(person.died / 100);
});

for (var century in byCentury) {
  var ages = byCentury[century].map(function(person) {
    return person.died - person.born;
  });
  console.log(century + ": " + average(ages));
}

// → 16: 43.5
//   17: 51.2
//   18: 52.8
//   19: 54.8
//   20: 84.7
//   21: 94

My question is around groupOf(element). What's going on here? "element" takes a value of 16,17,18,19,20 or 21 (as a result of function(person) {return Math.ceil(person.died / 100);}). a) What does groupOf(element) look like? groupOf was never defined. b) It seemed to me that I could substitute groupOf(element) with element but that's not true... Can someone help me understand what I'm not understanding? Thanks.

我的问题是围绕groupOf(元素)。这里发生了什么? “element”取值为16,17,18,19,20或21(作为函数(person)的结果{return Math.ceil(person.died / 100);})。 a)groupOf(element)是什么样的? groupOf从未定义过。 b)在我看来,我可以用元素替换groupOf(元素),但那不是真的......有人能帮助我理解我不理解的东西吗?谢谢。

2 个解决方案

#1


groupOf is defined. Its a parameter in var byCentury = groupBy(ancestry, /* function goes here */);

groupOf已定义。它的参数在var byCentury = groupBy(祖先,/ *函数在这里* /);

It's a bit difficult to see because of all the brackets. It's the same as doing:

由于所有括号,有点难以看到。它和做的一样:

var myFunctionAsAParameter = function(person) {
  return Math.ceil(person.died / 100);
}

Then...

var byCentury = groupBy(ancestry, myFunctionAsAParameter);

myFunctionAsAParameter does not execute until the JavaScript sees it with (), which you see happening in: groups[groupOf(element)].push(element);

myFunctionAsAParameter不会执行,直到JavaScript看到它(),你看到发生在:groups [groupOf(element)]。push(element);

So in this case, it will get executed a few times, each time with different person (determined by the foreach loop).

因此,在这种情况下,它将被执行几次,每次都与不同的人(由foreach循环确定)。

It IS a bit of a struggle to wrap your head around, but it's pretty powerful. Functions can take other functions as parameters. They'll only get executed when you add the (). Functions can also return other functions if they want. Now to really hurt your head. Functions can even return and accept themselves as parameters (known as recursion).

包裹你的头脑有点挣扎,但它非常强大。函数可以将其他函数作为参数。只有在添加()时才会执行它们。如果需要,函数也可以返回其他函数。现在要真的伤到你的头脑。函数甚至可以返回并接受自己作为参数(称为递归)。

#2


if you look closely at this snippet in your code

如果你仔细查看代码中的这个片段

var byCentury = groupBy(ancestry, function(person) { // element = function(person) {return Math.ceil(person.died / 100);}
  return Math.ceil(person.died / 100);
});

groupOf is nothing but this function which is passed as a second parameter to groupBy

groupOf只是这个函数,它作为第二个参数传递给groupBy

function(person) { // element = function(person) {return Math.ceil(person.died / 100);}
  return Math.ceil(person.died / 100);
}

since functions are just objects in javascript, you can pass them around to other functions as well.

因为函数只是javascript中的对象,所以你也可以将它们传递给其他函数。

#1


groupOf is defined. Its a parameter in var byCentury = groupBy(ancestry, /* function goes here */);

groupOf已定义。它的参数在var byCentury = groupBy(祖先,/ *函数在这里* /);

It's a bit difficult to see because of all the brackets. It's the same as doing:

由于所有括号,有点难以看到。它和做的一样:

var myFunctionAsAParameter = function(person) {
  return Math.ceil(person.died / 100);
}

Then...

var byCentury = groupBy(ancestry, myFunctionAsAParameter);

myFunctionAsAParameter does not execute until the JavaScript sees it with (), which you see happening in: groups[groupOf(element)].push(element);

myFunctionAsAParameter不会执行,直到JavaScript看到它(),你看到发生在:groups [groupOf(element)]。push(element);

So in this case, it will get executed a few times, each time with different person (determined by the foreach loop).

因此,在这种情况下,它将被执行几次,每次都与不同的人(由foreach循环确定)。

It IS a bit of a struggle to wrap your head around, but it's pretty powerful. Functions can take other functions as parameters. They'll only get executed when you add the (). Functions can also return other functions if they want. Now to really hurt your head. Functions can even return and accept themselves as parameters (known as recursion).

包裹你的头脑有点挣扎,但它非常强大。函数可以将其他函数作为参数。只有在添加()时才会执行它们。如果需要,函数也可以返回其他函数。现在要真的伤到你的头脑。函数甚至可以返回并接受自己作为参数(称为递归)。

#2


if you look closely at this snippet in your code

如果你仔细查看代码中的这个片段

var byCentury = groupBy(ancestry, function(person) { // element = function(person) {return Math.ceil(person.died / 100);}
  return Math.ceil(person.died / 100);
});

groupOf is nothing but this function which is passed as a second parameter to groupBy

groupOf只是这个函数,它作为第二个参数传递给groupBy

function(person) { // element = function(person) {return Math.ceil(person.died / 100);}
  return Math.ceil(person.died / 100);
}

since functions are just objects in javascript, you can pass them around to other functions as well.

因为函数只是javascript中的对象,所以你也可以将它们传递给其他函数。