JS中的高阶函数 [英] Higher-Order Functions in JS

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

问题描述

我正在学习JavaScript.我有一个问题.本书Eloquent JavaScript中的以下代码:

I'm learning JavaScript now. And I have some question. The following code from the book Eloquent JavaScript:

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

var numbers = [1, 2, 3, 4, 5],
    sum = 0;

forEach(numbers, function(number) {
    sum += number;
});

console.log(sum); 

此代码中发生了什么?在调用forEach函数时,如何确定数字是多少?如何从数字数组中提取数字?

What is happening in this code? How does the forEach function, when called, determine what is the number? How does it extract number from the numbers array?

推荐答案

此代码中发生了什么?

What is happening in this code?

以下是 forEach 函数的工作方式的描述:

Here's a description of how your forEach function works:

  1. 您的 forEach 函数接受两个参数,即数组和一个函数
  2. 它设置了一个 for 循环以遍历数组
  3. 然后,对于数组中的每个元素,它调用 action(array [i]),后者调用传入的函数并将其传递给一个数组元素. array [i] 从数组中获取下一项,然后 action(array [i])调用传入的函数,并将其从数组中传递出去.li>
  1. Your forEach function accepts two arguments, the array and a function
  2. It sets up a for loop to iterate through the array
  3. Then, for each element in the array it calls action(array[i]) which calls the passed in function and passes it one array element. array[i] gets the next item from the array and action(array[i]) calls the passed in function and passes it that next item from the array.

然后,当您这样调用时:

Then, when you call it like this:

forEach(numbers, function(number) {
    sum += number;
});

它将为您为 numbers 数组中的每个数字传递一次传递给您的函数,当它调用该函数时,它将从数组传递一个数字作为函数的第一个参数.

It will call the function you passed it once for each number in the numbers array and when it calls that function, it passed a number from the array as the first argument to your function.

让您感到困惑的一件事是传递给 forEach 的函数中的 number 参数,只是您为函数的第一个参数分配的名称.它可以命名为任何您想要的名称,并且与 forEach()函数定义中使用的任何名称无关.函数参数的声明只是分配给每个参数的标签,这使您更易于使用它们.您也可以这样做:

One thing that confuses some people is the number argument in your function you are passing to forEach is just a name you assign to the first argument of the function. It can be named anything you want and has nothing to do with any names used in the forEach() function definition. The declaration of arguments for functions are just labels assigned to each argument which make it easier for you to use them. You could have also done this:

forEach(numbers, function(whateverYouWant) {
    sum += whateverYouWant;
});

在调用forEach函数时,如何确定什么是数字?

How does the forEach function, when called, determine what is the number?

您的 for 循环一次遍历整个数组一个数字,并为数组中的每个数字调用一次回调函数.

Your for loop iterates through the whole array one number at a time and calls the callback function once for each number in the array.

它如何从数字数组中提取数字?

How does it extract number from the numbers array?

array [i] 是从数组中提取数字的位置.这是正常的数组索引,用于从 i 0 变化到数组长度减去1的数组中检索第i个值.

array[i] is where a number is extracted from the array. This is normal array indexing for retrieving the i th value from the array where i varies from 0 to the length of the array minus 1.

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

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