React - memo 2020-04-24- 2022-01-05 React-React-组件123456789101112131415161718192021function memo(fn) { return function () { const arg = Array.protoytype.slice.call(arguments); // 判断传入的函数有没有cache缓存 fn.cache = fn.cache || {}; // 判断传入的函数纯cache缓存有没有对应的参数 // 有,直接返回值 // 没有进行计算,并往函数的cache缓存存入计算的结果,下次直接返回值 return fn.cache[arg] ? fn.cache[arg] : (fn.cache[arg] = fn.call(this, arg)); };}// 功能函数var sqrt = function (num) { return Math.sqrt(num);};var memoSqart = memo(sqart);memoSqart(4); // 计算并存值memoSqart(4); // 直接返回缓存的值 I'm so cute. Please give me money.Post author: 彩鹏Post link: https://blog.gaocaipeng.com/2020/04/24/ng0i8w/Copyright Notice: All articles in this blog are licensed under unless otherwise stated.