高阶函数-Javascript [英] Higher order functions - Javascript

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

问题描述

我正在使用Eloquent Javascript.函数 count 以一个数组和一个测试函数( equals(x))作为参数,并返回该数组中测试函数返回true的元素数量.

I am working through Eloquent Javascript. The function count takes an array and a test function (equals(x)) as arguments, and returns the amount of elements in the array for which the test function returned true.

我了解这些功能的广泛使用方式,并且在逻辑上,传递给reduce的匿名函数的 total 参数的值为零.

I understand the broad way that these functions are working, and that logically the total argument to the anonymous function passed to reduce has a value of zero.

有人可以帮我看看总价值到底是从哪里来的吗?我想更清晰地了解一下.

Can someone help me to see where the value for total is coming from specifically though? I want to have a clearer picture in my mind.

function count(test, array) {
  return reduce(function(total, element) { // Where is the value for total coming from?
    return total + (test(element) ? 1 : 0);
  }, 0, array);
}

function equals(x) {
  return function(element) {return x === element;};
}

function countZeroes(array) {
  return count(equals(0), array);
}

从较早版本减少功能:

function reduce(combine, base, array) {
  forEach(array, function (element) {
    base = combine(base, element);
  });
  return base;
}

之前的forEach函数:

forEach Function from earlier:

function forEach(array, action) {
  for (var i = 0; i < array.length; i++)
    action(array[i]);
}

推荐答案

您传递来减少的3个参数是:

The 3 arguments you passed to reduce were:

{
    combine:function(total, element){...},
    base:0,
    array:array
}

该函数然后使用base并将其作为total参数传递给combine函数:

The function then takes base and passes it to the combine function as the total argument:

base = combine(base, element);

基本上,这里发生的是,对于您刚刚传递的数组中的每个元素(作为第三个参数array),该函数都会接受参数base并使用您提供的匿名函数对其进行递增(第一个检查元素是否通过test.最后,在遍历所有元素之后,它返回最终值base.

Basically, what is happening here is that for each element in the array you just passed (as the 3rd argument array) the function takes the argument base and increments it using the anonymous function you have provided (which first checks if the element passes test). Finally, after it has iterated over all the elements, it returns the final value of base.

也许这将有助于解释:

function count(test, testarray) {
  var anon = function(total, element) { // Where is the value for total coming from?
    return total + (test(element) ? 1 : 0);
  };
  //now anon is a function.
  return reduce(anon, 0, testarray);
}

让我们仔细研究一下函数调用和定义:

Let us look at the function invocation and definition closely:

return   reduce(anon   , 0   , testarray);
                  |      |     |
                  v      v     v
function reduce(combine, base, array) {
    combine;    //the function that is passed in as the first argument
    base;       //the number that is passed in as the second argument
    array;      //the array that is passed in as the third argument

每个anon0testarray都传递到该函数中.在函数内,可以通过函数定义中的参数名称访问它们的值.

The values of each of anon,0, and testarray, get passed into the function. Within the function, their values can be accessed by the parameter names in the function definition.

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

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