Created
October 29, 2017 00:59
-
-
Save cyan33/5acc81859ec9ca8e0f9a3db77ceacb8e to your computer and use it in GitHub Desktop.
the optimize callback method in underscore.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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