备忘录模式主要用于优化比较耗时的计算,经过将计算结果缓存到内存中,这样对于一样的输入值,下次只须要中内存中读取结果。git
const memento = func => {
const cache = {}
return function() {
let key = arguments[0]
return cache[key] || (cache[key] = func.apply(null, arguments))
}
}
复制代码
使用场景:github