账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue源码阅读 cached方法疑惑!
    21
    0

    在读vue源码时,遇到cached方法,如下:

    // 在src/shared/until.js中
    export function cached (fn: Function): Function {
      const cache = Object.create(null)
      return function cachedFn (str: string): any {
        const hit = cache[str]
        return hit || (cache[str] = fn(str))
      }
    }
    

    上面这个方法在很多其他方法中有运用,如

    //驼峰化以连字符分隔的字符
    const camelizeRE = /-(\w)/g
    export const camelize = cached((str: string): string => {
      return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
    })
    
    //大写字符串
    var capitalize = cached(function (str) {
      return str.charAt(0).toUpperCase() + str.slice(1)
    });

    网上查询了说是cached方式加快数据的读取速度,加做缓存策略

    但是并不能理解,有大神可以讲解下,该方法的原理吗?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 久别无恙 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部