_optimizeCb.js 886 B

123456789101112131415161718192021222324252627
  1. define(function () {
  2. // Internal function that returns an efficient (for current engines) version
  3. // of the passed-in callback, to be repeatedly applied in other Underscore
  4. // functions.
  5. function optimizeCb(func, context, argCount) {
  6. if (context === void 0) return func;
  7. switch (argCount == null ? 3 : argCount) {
  8. case 1: return function(value) {
  9. return func.call(context, value);
  10. };
  11. // The 2-argument case is omitted because we’re not using it.
  12. case 3: return function(value, index, collection) {
  13. return func.call(context, value, index, collection);
  14. };
  15. case 4: return function(accumulator, value, index, collection) {
  16. return func.call(context, accumulator, value, index, collection);
  17. };
  18. }
  19. return function() {
  20. return func.apply(context, arguments);
  21. };
  22. }
  23. return optimizeCb;
  24. });