Skip to content

Instantly share code, notes, and snippets.

@cyan33
Created October 29, 2017 00:59
Show Gist options
  • Save cyan33/5acc81859ec9ca8e0f9a3db77ceacb8e to your computer and use it in GitHub Desktop.
Save cyan33/5acc81859ec9ca8e0f9a3db77ceacb8e to your computer and use it in GitHub Desktop.
the optimize callback method in underscore.js
function optimizeCb(func, context, argCount) {
if (!context) return func
switch (argCount ? argCount : 3) {
case 1: return function(val) {
return func.call(context, val)
}
case 2: return function(val, other) {
return func.call(context, val, other)
}
case 3: return function(item, index, collection) {
return func.call(context, item, index, collection)
}
case 4: return function(accu, curr, index, collection) {
return func.call(context, accu, curr, index, collection)
}
}
return function() {
return func.apply(context, arguments)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment