简体   繁体   English

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

[英]Javascript – passing a function to an undefined function

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). 我的问题是关于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);}). “ element”的值为16,17,18,19,20或21(作为function(person)的结果{返回Math.ceil(person.died / 100);})。 a) What does groupOf(element) look like? a)groupOf(element)是什么样的? groupOf was never defined. groupOf从未定义。 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? b)在我看来,我可以用element代替groupOf(element),但事实并非如此……有人可以帮助我了解我不了解的内容吗? Thanks. 谢谢。

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中的对象,因此您也可以将它们传递给其他函数。

groupOf is defined. groupOf 定义。 Its a parameter in var byCentury = groupBy(ancestry, /* function goes here */); 它是var byCentury = groupBy(ancestry, /* function goes here */);一个参数var byCentury = groupBy(ancestry, /* function goes here */);

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); 在JavaScript用()看到它之前, myFunctionAsAParameter不会执行,您会在以下情况中看到它: 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). 函数甚至可以返回并接受自己作为参数(称为递归)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM