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);
};
};
我不太理解optimizeCb只传入了3个参数,但是里边的判断语句像下边这句
case 4: return function(accumulator, value, index, collection) {
return func.call(context, accumulator, value, index, collection);
};
这个内部函数的accumulator, value, index, collection这些参数是从哪里来的呢?
它返回的是一个函数本身,这个返回的函数肯定是要拿到别的地方运行的,这些参数当然就是运行的时候输入的啦。