onion洋葱模型

const log = console.log;
function m1(next) { log(1); next(); log(2); }
function m2(next) { log(3); next(); log(4); }
function m3(next) { log(5); next(); log(6); }

function end() { log('end'); }
function m3_end() { m3(end); }
function m2_end() { m2(m3_end); }
m1(m2_end);
// 打印顺序 1 3 5 end 6 4 2

利用高阶函数

fn => compose();
高阶函数:

  1. 入口参数是函数;
  2. 返回函数;
  3. 两者皆备;
const log = console.log;
function m1(next) { log(1); next(); log(2); }
function m2(next) { log(3); next(); log(4); }
function m3(next) { log(5); next(); log(6); }

let ms = [];
use(m1); use(m2); use(m3);
function use(mx) {
  ms.push(mx);
}

let index = 0;
function compose(index = 0) {
  if (index === ms.length) {
    return () => { log('end') };
  }
  return () => {
    ms[index](compose(index + 1));
  };
}

let fn = compose();
fn();

相关推荐

  1. onion洋葱模型

    2024-04-30 09:36:03       10 阅读
  2. Tor网络-洋葱网络(匿名通信的网络)学习记录-简介

    2024-04-30 09:36:03       111 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-30 09:36:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-30 09:36:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-30 09:36:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-30 09:36:03       18 阅读

热门阅读

  1. 程序设计:C语言 UNIX/LINUX 环境变量替换

    2024-04-30 09:36:03       11 阅读
  2. CentOS 7 安装 Puppeteer & Google Chrome

    2024-04-30 09:36:03       10 阅读
  3. 数据库三范式

    2024-04-30 09:36:03       10 阅读
  4. 构建嵌入空间

    2024-04-30 09:36:03       10 阅读
  5. Zephyr storage存储子系统系统学习记录

    2024-04-30 09:36:03       11 阅读
  6. AnolisOS8.8基于yum安装mariadb并配置远程访问

    2024-04-30 09:36:03       10 阅读
  7. js执行顺序

    2024-04-30 09:36:03       8 阅读
  8. Visual Studio Installer 运行python 汉字

    2024-04-30 09:36:03       10 阅读