函数缓存
- 把参数和对应的结果数据存在一个对象中,调用时判断参数对应的数据是否存在,存在就返回对应的结果数据,否则就返回计算结果
const memoize = function (func, content) {let cache = Object.create(null)content = content || thisreturn (...key) => {if (!cache[key]) {cache[key] = func.apply(content, key)}return cache[key]}}function add(a, b) {return a + b}const calc = memoize(add)const num1 = calc(100, 200)const num2 = calc(100, 200) // 缓存得到的结果
