onion洋葱模型

javascript 复制代码
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. 两者皆备;
javascript 复制代码
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();
相关推荐
Go away, devil39 分钟前
Java-----集合
java·开发语言
程序员爱钓鱼40 分钟前
Python 编程实战 · 实用工具与库 — Flask 路由与模板
前端·后端·python
VBA63373 小时前
VBA即用型代码手册:利用函数保存为PDF文件UseFunctionSaveAsPDF
开发语言
say_fall3 小时前
C语言编程实战:每日刷题 - day2
c语言·开发语言·学习
合作小小程序员小小店4 小时前
web开发,在线%超市销售%管理系统,基于idea,html,jsp,java,ssh,sql server数据库。
java·前端·sqlserver·ssh·intellij-idea
上去我就QWER4 小时前
Qt快捷键“魔法师”:QKeySequence
开发语言·c++·qt
不爱学英文的码字机器5 小时前
重塑 Web 性能:用 Rust 与 WASM 构建“零开销”图像处理器
前端·rust·wasm
浩星5 小时前
react的框架UmiJs(五米)
前端·javascript·react.js
Pluto_CSND6 小时前
Java中的静态代理与动态代理(Proxy.newProxyInstance)
java·开发语言
子醉7 小时前
推荐一种适合前端开发使用的解决本地跨域问题的办法
前端