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); }; };
1 回答

HUH函数
TA贡献1836条经验 获得超4个赞
这不就是闭包吗,当你调用外层函数的时候,返回了一个新的函数,然后你再给返回的函数传值
例如:
function fn1(a){ return function(b){ console.log(a+b) } } var fn2 = fn1('hello') //这时候的值是返回的函数 fn2('world') //这时候执行里面的函数 得到 helloworld
添加回答
举报
0/150
提交
取消