method.js 860 B

1234567891011121314151617181920212223242526272829303132333435
  1. var baseInvoke = require('./_baseInvoke'),
  2. baseRest = require('./_baseRest');
  3. /**
  4. * Creates a function that invokes the method at `path` of a given object.
  5. * Any additional arguments are provided to the invoked method.
  6. *
  7. * @static
  8. * @memberOf _
  9. * @since 3.7.0
  10. * @category Util
  11. * @param {Array|string} path The path of the method to invoke.
  12. * @param {...*} [args] The arguments to invoke the method with.
  13. * @returns {Function} Returns the new invoker function.
  14. * @example
  15. *
  16. * var objects = [
  17. * { 'a': { 'b': _.constant(2) } },
  18. * { 'a': { 'b': _.constant(1) } }
  19. * ];
  20. *
  21. * _.map(objects, _.method('a.b'));
  22. * // => [2, 1]
  23. *
  24. * _.map(objects, _.method(['a', 'b']));
  25. * // => [2, 1]
  26. */
  27. var method = baseRest(function(path, args) {
  28. return function(object) {
  29. return baseInvoke(object, path, args);
  30. };
  31. });
  32. module.exports = method;