跳至主要內容

高阶函数

Mr.Hope...小于 1 分钟JavaScript

高阶函数英文叫 Higher-order function。

JavaScript 的函数其实都指向某个变量。既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数。

一个最简单的高阶函数:

const add = (x, y, f) => f(x) + f(y);

或者写作

function add(x, y, f) {
  return f(x) + f(y);
}

当我们调用 add(-5, 6, Math.abs) 时,参数 xyf 分别接收 -56 和函数 Math.abs,根据函数定义,我们可以推导计算过程为:

x = -5;
y = 6;
f = Math.abs;
f(x) + f(y) ==> Math.abs(-5) + Math.abs(6) ==> 11;
return 11;

用代码验证一下:

"use strict";

const add = (x, y, f) => f(x) + f(y);
const x = add(-5, 6, Math.abs);

console.log(x); // 11

编写高阶函数,就是让函数的参数能够接收别的函数。

评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3