高级函数将函数传递给另一个函数时的解释 [英] High order functions explanation when passing a function to another function

查看:94
本文介绍了高级函数将函数传递给另一个函数时的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们有forEach函数来记录每一个项目阵列。

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





$ b因此我们有一个名为'数字'的数组和另一个名为'sum'的变量,设置为0.
当我将'function(number)'传递给动作参数时,我迷失方向。这有什么作用,它是如何工作的?我不明白它的价值。有人可以把它分解吗?

  var numbers = [1,2,3,4,5],sum = 0; 
forEach(数字,函数(数字){
sum + = number;
});

console.log(sum);
//→15


解决方案

我想你没有掌握呼叫的语法结构。人们也可以写出:

  var numbers = [1,2,3,4,5]; 
var sum = 0;
函数addToSum(数字){
sum + = number;
}

forEach(numbers,addToSum);

我希望清楚这里发生了什么。我们有一个函数 addToSum ,并将该函数传递给 forEach 函数 - 我们通过它的参数名称来引用它 action ,并将在循环体中传入的数组的每个元素调用它: action(array [i])。关于这个抽象的很多事情是我们可以将任意函数传递给 forEach ...



现在的语法?首先,它将用函数表达式替换函数声明。函数表达式就是一个表达式,它的计算结果是一个函数对象,就像数组文字 [1,2,3,4,5] 计算数组对象一样。它也称为匿名函数,因为它没有名称。我们把这个值赋给一个变量:

  var numbers = [1,2,3,4,5]; 
var sum = 0;
var addTosum = function(number){
sum + = number;
}

forEach(numbers,addToSum);

直到这里才有真正的变化。现在,为什么我们需要这个变量?我们并不需要这些标识符指向我们使用表达式创建的值。我们现在只是将表达式直接放在 forEach 函数调用中 - 仍然传递相同的值( forEach 差异):

  var sum = 0; 

forEach([1,2,3,4,5],函数(数字){
sum + = number;
});


I need help breaking down line by line what happens in this example (an excerpt from Eloquent Javascript):

We have the forEach function which logs every item in an array. I understand how that works.

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

So we have an array called 'numbers' and another variable called 'sum' set to 0. Where I get lost is when 'function(number)' gets passed into the the action parameter. What does that do and how does it work? I don't see what it's value is. Can someone break this down?

var numbers = [1, 2, 3, 4, 5], sum = 0;
forEach(numbers, function(number) {
  sum += number;
});

console.log(sum);
// → 15

解决方案

I think you're not grasping the syntactic structure of the call. One could equally write

var numbers = [1, 2, 3, 4, 5];
var sum = 0;
function addToSum(number) {
  sum += number;
}

forEach(numbers, addToSum);

I hope it is clear what is happening in here. We have a function addToSum, and pass that function to the forEach function - where we refer to it by its parameter name action, and will call it with each element of the passed array in the body of the loop: action(array[i]). The great deal about this abstraction is that we can pass any arbitrary function to forEach

So what is this odd syntax now? First, it is replacing the function declaration by a function expression. A function expression is just an expression that evaluates to a function object, just as the array literal [1, 2, 3, 4, 5] evaluates to the array object. It's also called an anonymous function as it does not have a name. We take this value and assign it to a variable:

var numbers = [1, 2, 3, 4, 5];
var sum = 0;
var addTosum = function(number) {
  sum += number;
}

forEach(numbers, addToSum);

Nothing has really changed until here. Now, why would we need that variable? We don't really need those identifiers pointing to the values that we created with the expressions. We now just put the expressions directly in the forEach function call - still passing the same values (forEach does not spot a difference):

var sum = 0;

forEach([1, 2, 3, 4, 5], function(number) {
  sum += number;
});

这篇关于高级函数将函数传递给另一个函数时的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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