登录

javascript - underscore中源码optimizeCb如何理解?

  var optimizeCb = function(func, context, argCount) {
    if (context === void 0) return func;
    switch (argCount == null ? 3 : argCount) {
      case 1: return function(value) {
        return func.call(context, value);
      };
      case 2: return function(value, other) {
        return func.call(context, value, other);
      };
      case 3: return function(value, index, collection) {
        return func.call(context, value, index, collection);
      };
      case 4: return function(accumulator, value, index, collection) {
        return func.call(context, accumulator, value, index, collection);
      };
    }
    return function() {
      return func.apply(context, arguments);
    };
  };

上次同事说这段代码叫 偏函数 ,然后上网查了一些概念 http://www.vaikan.com/currying-partial-application/
之后又了解到 Higher-Order Functions,能否解释一下这些概念?以及上面的函数是不是偏函数?

# JavaScript
怪我咯怪我咯2185 天前441 次浏览

全部回复(1) 我要回复

  • 阿神

    阿神2017-04-10 16:04:13

    这个之前讨论过。讨论的结果见这个链接:
    关于underscore.js中optimizeCb函数中是否需要switch的问题

    回复
    0
  • 取消回复发送