methodOf.js 912 B

1234567891011121314151617181920212223242526272829303132
  1. var invokePath = require('../internal/invokePath'),
  2. restParam = require('../function/restParam');
  3. /**
  4. * The opposite of `_.method`; this method creates a function that invokes
  5. * the method at a given path on `object`. Any additional arguments are
  6. * provided to the invoked method.
  7. *
  8. * @static
  9. * @memberOf _
  10. * @category Utility
  11. * @param {Object} object The object to query.
  12. * @param {...*} [args] The arguments to invoke the method with.
  13. * @returns {Function} Returns the new function.
  14. * @example
  15. *
  16. * var array = _.times(3, _.constant),
  17. * object = { 'a': array, 'b': array, 'c': array };
  18. *
  19. * _.map(['a[2]', 'c[0]'], _.methodOf(object));
  20. * // => [2, 0]
  21. *
  22. * _.map([['a', '2'], ['c', '0']], _.methodOf(object));
  23. * // => [2, 0]
  24. */
  25. var methodOf = restParam(function(object, args) {
  26. return function(path) {
  27. return invokePath(object, path, args);
  28. };
  29. });
  30. module.exports = methodOf;