备忘录模式

https://blog.csdn.net/KlausLily/article/details/103046377

const baseOperateController = function() {
    // 缓存对象
    const cache = {};
    // 基础函数
    return function(params, fn, cb) {
        // 判断是否在缓存下
        if(cache[params]) {
            // 从缓存中读取并传递给业务函数
            fn(cache[params])
        }else {
            // 没有缓存则将控制权转移给外部回调函数,并传递相应参数
            cb && cb(params).then((res => {
                cache[params] = res.data;
            })).catch(err => console.log(err))
        }
    }
}

// 使用
baseOperateController(params, showView, asyncFn)

 可以传参数模式

const baseOperateController = (function() {
  // 缓存对象
  const cache = {}
  // 基础函数
  return (params, fn, cb, ...rest) => {
    // 判断是否在缓存下
    console.log('arguments', rest)
    if (cache[params]) {
      // 从缓存中读取并传递给业务函数
      fn.call(this, cache[params], rest)
    } else {
      // 没有缓存则将控制权转移给外部回调函数,并传递相应参数
      cb &&
        cb
          .call(this, params, rest)
          .then(res => {
            cache[params] = res.records
          })
          .catch(err => console.log(err))
    }
  }
})()
原文地址:https://www.cnblogs.com/TTblog5/p/13151701.html