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);
};
};
HUH函数
相关分类