assign.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var assignWith = require('../internal/assignWith'),
  2. baseAssign = require('../internal/baseAssign'),
  3. createAssigner = require('../internal/createAssigner');
  4. /**
  5. * Assigns own enumerable properties of source object(s) to the destination
  6. * object. Subsequent sources overwrite property assignments of previous sources.
  7. * If `customizer` is provided it's invoked to produce the assigned values.
  8. * The `customizer` is bound to `thisArg` and invoked with five arguments:
  9. * (objectValue, sourceValue, key, object, source).
  10. *
  11. * **Note:** This method mutates `object` and is based on
  12. * [`Object.assign`](http://ecma-international.org/ecma-262/6.0/#sec-object.assign).
  13. *
  14. * @static
  15. * @memberOf _
  16. * @alias extend
  17. * @category Object
  18. * @param {Object} object The destination object.
  19. * @param {...Object} [sources] The source objects.
  20. * @param {Function} [customizer] The function to customize assigned values.
  21. * @param {*} [thisArg] The `this` binding of `customizer`.
  22. * @returns {Object} Returns `object`.
  23. * @example
  24. *
  25. * _.assign({ 'user': 'barney' }, { 'age': 40 }, { 'user': 'fred' });
  26. * // => { 'user': 'fred', 'age': 40 }
  27. *
  28. * // using a customizer callback
  29. * var defaults = _.partialRight(_.assign, function(value, other) {
  30. * return _.isUndefined(value) ? other : value;
  31. * });
  32. *
  33. * defaults({ 'user': 'barney' }, { 'age': 36 }, { 'user': 'fred' });
  34. * // => { 'user': 'barney', 'age': 36 }
  35. */
  36. var assign = createAssigner(function(object, source, customizer) {
  37. return customizer
  38. ? assignWith(object, source, customizer)
  39. : baseAssign(object, source);
  40. });
  41. module.exports = assign;