partial.js 1012 B

12345678910111213141516171819202122232425
  1. define(['./restArguments', './_executeBound', './underscore'], function (restArguments, _executeBound, underscore) {
  2. // Partially apply a function by creating a version that has had some of its
  3. // arguments pre-filled, without changing its dynamic `this` context. `_` acts
  4. // as a placeholder by default, allowing any combination of arguments to be
  5. // pre-filled. Set `_.partial.placeholder` for a custom placeholder argument.
  6. var partial = restArguments(function(func, boundArgs) {
  7. var placeholder = partial.placeholder;
  8. var bound = function() {
  9. var position = 0, length = boundArgs.length;
  10. var args = Array(length);
  11. for (var i = 0; i < length; i++) {
  12. args[i] = boundArgs[i] === placeholder ? arguments[position++] : boundArgs[i];
  13. }
  14. while (position < arguments.length) args.push(arguments[position++]);
  15. return _executeBound(func, bound, this, this, args);
  16. };
  17. return bound;
  18. });
  19. partial.placeholder = underscore;
  20. return partial;
  21. });