methodOf.js 912 B

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