外部函数接收三个参数,里面的函数只需要一个参数或者需要四个参数的时候,里面的函数的参数怎么被赋值的?

  var optimizeCb = function(func, context, argCount) {

    // 如果没有指定 this 指向,则返回原函数

    if (context === void 0) return func;


    switch (argCount == null ? 3 : argCount) {

      case 1: return function(value) {                        //这里的value从哪里来的

        return func.call(context, value);       

      };

      case 2: return function(value, other) {                 //这里的other从哪里来的

        return func.call(context, value, other);  

      };


      // 如果有指定 this,但没有传入 argCount 参数

      // 则执行以下 case

      // _.each、_.map

      case 3: return function(value, index, collection) {     //还有这里的index,collection

        return func.call(context, value, index, collection);

      };


      // _.reduce、_.reduceRight

      case 4: return function(accumulator, value, index, collection) {    //这里的accumulator

        return func.call(context, accumulator, value, index, collection);

      };

    }

    return function() {

      return func.apply(context, arguments);

    };

  };


慕沐林林
浏览 506回答 1
1回答

HUH函数

这不就是闭包吗,当你调用外层函数的时候,返回了一个新的函数,然后你再给返回的函数传值例如: function fn1(a){   return function(b){     console.log(a+b)    }  }   var fn2 = fn1('hello') //这时候的值是返回的函数  fn2('world') //这时候执行里面的函数 得到 helloworld
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript