简体   繁体   English

javascript中简单的高阶函数的示例

[英]An example of a simple higher-order function in javascript

While going through Eloquent Javascript (Chapter 6) there is a reference to higher-order functions in Javascript. 在阅读Eloquent Javascript(第6章)时 ,引用了Javascript中的高阶函数。 While there is an example provided in Chapter 3, I believe it could be a bit simpler since I still don't fully understand the concept. 尽管第3章提供了一个示例,但我认为它可能会更简单一些,因为我仍然不完全了解该概念。 After searching the web I can't seem to find any succinct examples of a higher-order function. 在网上搜索之后,我似乎找不到任何高阶函数的简洁示例。

I'd like to see a basic/simple higher-order function in Javascript that will explain the concept. 我想在Javascript中看到一个基本/简单的高阶函数来解释这个概念。

Higher functions are concepts from functional programming . 更高的功能是功能编程的概念。 In briefly, a higher function is a function which takes another function as parameter. 简而言之,高级函数是将另一个函数作为参数的函数。 In javascript, some higher functions are added recently. 在javascript中,最近添加了一些更高的功能。

Array.prototype.reduce 
//With this function, we can do some funny things.
function sum(array){
    return array.reduce(function(a, b){ return a + b; }, 0);
}

So, in the above sample, reduce is a higher order function, it takes another function, the anonymous function in the sample, as a parameter. 因此,在上面的示例中, reduce是一个高阶函数,它将另一个函数(示例中的匿名函数)作为参数。 The signature of reduce looks like this reduce的签名看起来像这样

reduce(func, init);
//func is a function takes two parameter and returns some value.
// init is the initial value which would be passed to func
//when calling reduce, some thing happen

//step 1.
[1, 2, 3, 4, 5].reduce(function(a, b){ return a + b }, 0);
//step 2.
[2, 3, 4, 5].reduce(function(a, b){ return a + b}, 0 + 1);
//step 3.
[3, 4, 5].reduce(function(a, b){ return a + b}, 0 + 1 + 2);
//...

As you can see, reduce iterate an array, and apply the func with init and first element of that array, then bind the result to init . 如您所见, reduce对数组的迭代,然后将funcinit和该数组的第一个元素一起应用,然后将结果绑定到init

Another higher order funciton is filter . 另一个更高阶的函数是filter

Array.prototype.filter
//As the name indicates, it filter out some unwanted values from an Aarry. It also takes a function, which returns a boolean value, true for keeping this element.
[1, 2, 3, 4, 5].filter(function(ele){ return ele % 2 == 0; });

With the above two examples, I have to say higher order function is not that much easy to understand, especially reduce . 对于以上两个示例,不得不说高阶函数不是那么容易理解,尤其是reduce But that's not complex , with higher order function, actually your code would be more clean and readable. 但这并不复杂 ,具有高阶函数,实际上您的代码将更清晰易读。 Take the filter as example, it tells people that it throws all odd numbers away. filter为例,它告诉人们它将所有奇数都丢掉了。

Here I'd like to implement a simple filter function to show you how. 在这里,我想实现一个简单的filter功能,向您展示如何。

function filter(array, func){
    var output = [];
    for(var i = 0; i < array.length; i++){
      if(func(array[i])) output.push(array[i]);
    }
    return output;
}

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

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