简体   繁体   English

JS中的高阶函数

[英]Higher-Order Functions in JS

I'm learning JavaScript now. 我正在学习JavaScript。 And I have some question. 我有一个问题。 The following code from the book Eloquent JavaScript: 本书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); 

What is happening in this code? 这段代码中发生了什么? How does the forEach function, when called, determine what is the number? 在调用forEach函数时,如何确定数字是多少? How does it extract number from the numbers array? 它如何从数字数组中提取数字?

What is happening in this code? 这段代码中发生了什么?

Here's a description of how your forEach function works: 这是您的forEach函数工作方式的描述:

  1. Your forEach function accepts two arguments, the array and a function 您的forEach函数接受两个参数,即数组和函数
  2. It sets up a for loop to iterate through the array 它设置了一个for循环来遍历数组
  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. 然后,对于数组中的每个元素,它将调用action(array[i]) ,后者将调用传入的函数并将其传递给一个数组元素。 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. array[i]从数组中获取下一项, action(array[i])调用传入的函数,并从数组中传递该项。

Then, when you call it like this: 然后,当您这样调用时:

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

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. 它将为您在numbers数组中的每个数字调用一次传递给您的函数,并且在调用该函数时,它将从数组传递一个数字作为函数的第一个参数。

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. 让人困惑的一件事是传递给forEach函数中的number参数只是为该函数的第一个参数分配的名称。 It can be named anything you want and has nothing to do with any names used in the forEach() function definition. 它可以命名为任何您想要的名称,并且与forEach()函数定义中使用的任何名称无关。 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;
});

How does the forEach function, when called, determine what is the number? 在调用forEach函数时,如何确定数字是多少?

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. 您的for循环一次遍历整个数组一个数字,并为数组中的每个数字调用一次回调函数。

How does it extract number from the numbers array? 它如何从数字数组中提取数字?

array[i] is where a number is extracted from the array. array[i]是从数组中提取数字的位置。 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. 这是用于从其中阵列中检索第i个值正常数组索引i从变化0至阵列减去1的长度。

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

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