vue功能函数----柯里化函数

1. 缓存策略(搞快点)

function cached(fn) {
// 将现有的 null deepcopy给 cache, 建立缓存对象
var cache = Object.create(null);
return (function cachedFn(str) {
// 将 参数名为 str 的键值从 cache 中取出
var hit = cache[str];
// 若hit有值, 则返回已缓存的处理结果 || 若hit不存在, 则返回一个将 fn 中的参数 str 做为缓存对象的键名, fn 的调用结果作为键值的 键值(等同于返回缓存的处理结果)
return hit || (cache[str] = fn(str))
  })
}

 

2. 兼容例如 PhantomJS1.x 的环境绑定context, 现代浏览器还是用bind()比柯里化性能好

function polyfillBind(fn, ctx) {
  function boundFn(a) {
    var l = arguments.length;
    return l
      // ***
      ? l > 1
    // ***
      ? fn.apply(ctx, arguments)
      : fn.call(ctx, a)
      // ***
      : fn.call(ctx)
  }
  boundFn._length = fn.length;
  return boundFn
}

 

3. 待更

posted @ 2020-05-12 11:36  Hybelkaniner  阅读(607)  评论(0)    收藏  举报