学习vue源码笔记

短命女 2022-09-09 14:52 256阅读 0赞
  1. function cached(fn) {
  2. var cache = Object.create(null)
  3. return function cachedFn(str) {
  4. var hit = cache[str]
  5. return hit || (cache[str] = fn(str))
  6. }
  7. }
  8. /** * Camelize a hyphen-delimited string. */
  9. var camelizeRE = /-(\w)/g
  10. var camelize = cached(function (str) {
  11. return str.replace(camelizeRE, function (_, c) {
  12. return c ? c.toUpperCase() : ""
  13. })
  14. })

学习第169-185行代码,
1.cached利用闭包做缓存。
2.发现当replace第二个参数为函数时,不止有一个参数,当repalce正则里面有小括号时候会有四个参数,第一个为匹配到的字符串,第二个为小括号匹配到的,第三个为开始匹配的索引位置,第四个为原始字符串,如果没有小括号,则参数为三个。

发表评论

表情:
评论列表 (有 0 条评论,256人围观)

还没有评论,来说两句吧...

相关阅读