返回 memoized(缓存的)函数。
使用 Object.create(null)
创建一个没有 Object.prototype
的空对象(这样如果输入值类似 'hasOwnProperty'
,那么这些属性就不会被解析)。
通过首先检查该特定输入值的函数输出是否已经被缓存,如果没有,则返回一个函数,该函数将作为单个参数提供给 memoized 函数。
const memoize = fn => { const cache = Object.create(null); return value => cache[value] || (cache[value] = fn(value)); };
// See the `anagrams` snippet. const anagramsCached = memoize(anagrams); anagramsCached('javascript'); // takes a long time anagramsCached('javascript'); // returns virtually instantly since it's now cached
更多代码 JavaScript 实用代码片段 请查看 https://www.html.cn/30-seconds-of-code/
最新评论
写的挺好的
有没有兴趣翻译 impatient js? https://exploringjs.com/impatient-js/index.html
Flexbox playground is so great!
感谢总结。
awesome!
这个好像很早就看到类似的文章了
比其他的教程好太多了
柯理化讲的好模糊…没懂