【进阶 6-1 期】JavaScript 高阶函数浅析

更新:谢谢你们的支持,最近折腾了一个博客官网出来,方便你们系统阅读,后续会有更多内容和更多优化,猛戳这里查看前端

------ 如下是正文 ------git

引言

本期开始介绍 JavaScript 中的高阶函数,在 JavaScript 中,函数是一种特殊类型的对象,它们是 Function objects。那什么是高阶函数呢?本节将经过高阶函数的定义来展开介绍。github

高阶函数

高阶函数英文叫 Higher-order function,它的定义很简单,就是至少知足下列一个条件的函数:面试

  • 接受一个或多个函数做为输入
  • 输出一个函数

也就是说高阶函数是对其余函数进行操做的函数,能够将它们做为参数传递,或者是返回它们。 简单来讲,高阶函数是一个接收函数做为参数传递或者将函数做为返回值输出的函数。c#

函数做为参数传递

JavaScript 语言中内置了一些高阶函数,好比 Array.prototype.map,Array.prototype.filter 和 Array.prototype.reduce,它们接受一个函数做为参数,并应用这个函数到列表的每个元素。咱们来看看使用它们与不使用高阶函数的方案对比。数组

Array.prototype.map

map() 方法建立一个新数组,其结果是该数组中的每一个元素都调用一个提供的函数后返回的结果,原始数组不会改变。传递给 map 的回调函数(callback)接受三个参数,分别是 currentValue、index(可选)、array(可选),除了 callback 以外还能够接受 this 值(可选),用于执行 callback 函数时使用的this 值。闭包

来个简单的例子方便理解,如今有一个数组 [1, 2, 3, 4],咱们想要生成一个新数组,其每一个元素皆是以前数组的两倍,那么咱们有下面两种使用高阶和不使用高阶函数的方式来实现。函数

不使用高阶函数

// 木易杨
const arr1 = [1, 2, 3, 4];
const arr2 = [];
for (let i = 0; i < arr1.length; i++) {
  arr2.push( arr1[i] * 2);
}

console.log( arr2 );
// [2, 4, 6, 8]
console.log( arr1 );
// [1, 2, 3, 4]
复制代码

使用高阶函数

// 木易杨
const arr1 = [1, 2, 3, 4];
const arr2 = arr1.map(item => item * 2);

console.log( arr2 );
// [2, 4, 6, 8]
console.log( arr1 );
// [1, 2, 3, 4]
复制代码

Array.prototype.filter

filter() 方法建立一个新数组, 其包含经过提供函数实现的测试的全部元素,原始数组不会改变。接收的参数和 map 是同样的,其返回值是一个新数组、由经过测试的全部元素组成,若是没有任何数组元素经过测试,则返回空数组。post

来个例子介绍下,如今有一个数组 [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4],咱们想要生成一个新数组,这个数组要求没有重复的内容,即为去重。学习

不使用高阶函数

const arr1 = [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4];
const arr2 = [];
for (let i = 0; i < arr1.length; i++) {
  if (arr1.indexOf( arr1[i] ) === i) {
    arr2.push( arr1[i] );
  }
}

console.log( arr2 );
// [1, 2, 3, 5, 4]
console.log( arr1 );
// [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4]
复制代码

使用高阶函数

const arr1 = [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4];
const arr2 = arr1.filter( (element, index, self) => {
    return self.indexOf( element ) === index;
});

console.log( arr2 );
// [1, 2, 3, 5, 4]
console.log( arr1 );
// [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4]
复制代码

Array.prototype.reduce

reduce() 方法对数组中的每一个元素执行一个提供的 reducer 函数(升序执行),将其结果汇总为单个返回值。传递给 reduce 的回调函数(callback)接受四个参数,分别是累加器 accumulator、currentValue、currentIndex(可选)、array(可选),除了 callback 以外还能够接受初始值 initialValue 值(可选)。

  • 若是没有提供 initialValue,那么第一次调用 callback 函数时,accumulator 使用原数组中的第一个元素,currentValue 便是数组中的第二个元素。 在没有初始值的空数组上调用 reduce 将报错。

  • 若是提供了 initialValue,那么将做为第一次调用 callback 函数时的第一个参数的值,即 accumulator,currentValue 使用原数组中的第一个元素。

来个简单的例子介绍下,如今有一个数组 [0, 1, 2, 3, 4],须要计算数组元素的和,需求比较简单,来看下代码实现。

不使用高阶函数

const arr = [0, 1, 2, 3, 4];
let sum = 0;
for (let i = 0; i < arr.length; i++) {
  sum += arr[i];
}

console.log( sum );
// 10
console.log( arr );
// [0, 1, 2, 3, 4]
复制代码

使用高阶函数

无 initialValue 值
const arr = [0, 1, 2, 3, 4];
let sum = arr.reduce((accumulator, currentValue, currentIndex, array) => {
  return accumulator + currentValue;
});

console.log( sum );
// 10
console.log( arr );
// [0, 1, 2, 3, 4]
复制代码

上面是没有 initialValue 的状况,代码的执行过程以下,callback 总共调用四次。

callback accumulator currentValue currentIndex array return value
first call 0 1 1 [0, 1, 2, 3, 4] 1
second call 1 2 2 [0, 1, 2, 3, 4] 3
third call 3 3 3 [0, 1, 2, 3, 4] 6
fourth call 6 4 4 [0, 1, 2, 3, 4] 10
有 initialValue 值

咱们再来看下有 initialValue 的状况,假设 initialValue 值为 10,咱们看下代码。

const arr = [0, 1, 2, 3, 4];
let sum = arr.reduce((accumulator, currentValue, currentIndex, array) => {
  return accumulator + currentValue;
}, 10);

console.log( sum );
// 20
console.log( arr );
// [0, 1, 2, 3, 4]
复制代码

代码的执行过程以下所示,callback 总共调用五次。

callback accumulator currentValue currentIndex array return value
first call 10 0 0 [0, 1, 2, 3, 4] 10
second call 10 1 1 [0, 1, 2, 3, 4] 11
third call 11 2 2 [0, 1, 2, 3, 4] 13
fourth call 13 3 3 [0, 1, 2, 3, 4] 16
fifth call 16 4 4 [0, 1, 2, 3, 4] 20

函数做为返回值输出

这个很好理解,就是返回一个函数,下面直接看两个例子来加深理解。

isType 函数

咱们知道在判断类型的时候能够经过 Object.prototype.toString.call 来获取对应对象返回的字符串,好比:

let isString = obj => Object.prototype.toString.call( obj ) === '[object String]';

let isArray = obj => Object.prototype.toString.call( obj ) === '[object Array]';

let isNumber = obj => Object.prototype.toString.call( obj ) === '[object Number]';
复制代码

能够发现上面三行代码有不少重复代码,只须要把具体的类型抽离出来就能够封装成一个判断类型的方法了,代码以下。

let isType = type => obj => {
  return Object.prototype.toString.call( obj ) === '[object ' + type + ']';
}

isType('String')('123');		// true
isType('Array')([1, 2, 3]);	// true
isType('Number')(123);			// true

复制代码

这里就是一个高阶函数,由于 isType 函数将 obj => { ... } 这一函数做为返回值输出。

add 函数

咱们看一个常见的面试题,用 JS 实现一个无限累加的函数 add,示例以下:

add(1); // 1
add(1)(2);  // 3
add(1)(2)(3); // 6
add(1)(2)(3)(4); // 10 

// 以此类推

复制代码

咱们能够看到结构和上面代码有些相似,都是将函数做为返回值输出,而后接收新的参数并进行计算。

咱们知道打印函数时会自动调用 toString()方法,函数 add(a) 返回一个闭包 sum(b),函数 sum() 中累加计算 a = a + b,只须要重写sum.toString()方法返回变量 a 就能够了。

function add(a) {
    function sum(b) { // 使用闭包
    	a = a + b; // 累加
    	return sum;
    }
    sum.toString = function() { // 重写toString()方法
        return a;
    }
    return sum; // 返回一个函数
}

add(1); // 1
add(1)(2);  // 3
add(1)(2)(3); // 6
add(1)(2)(3)(4); // 10 

复制代码

思考题

已知以下数组,编写一个程序将数组扁平化去并除其中重复部分数据,最终获得一个升序且不重复的数组

var arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];

参考解析:扁平化并去重

参考文章

理解 JavaScript 中的高阶函数

Array.prototype.map()

Array.prototype.filter()

Array.prototype.reduce()

文章穿梭机

交流

进阶系列文章汇总以下,以为不错点个Star,欢迎 加群 互相学习。

github.com/yygmind/blo…

我是木易杨,公众号「高级前端进阶」做者,跟着我每周重点攻克一个前端面试重难点。接下来让我带你走进高级前端的世界,在进阶的路上,共勉!

相关文章
相关标签/搜索