assignIn.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var assignValue = require('./_assignValue'),
  2. copyObject = require('./_copyObject'),
  3. createAssigner = require('./_createAssigner'),
  4. isArrayLike = require('./isArrayLike'),
  5. isPrototype = require('./_isPrototype'),
  6. keysIn = require('./keysIn');
  7. /** Used for built-in method references. */
  8. var objectProto = Object.prototype;
  9. /** Built-in value references. */
  10. var propertyIsEnumerable = objectProto.propertyIsEnumerable;
  11. /** Detect if properties shadowing those on `Object.prototype` are non-enumerable. */
  12. var nonEnumShadows = !propertyIsEnumerable.call({ 'valueOf': 1 }, 'valueOf');
  13. /**
  14. * This method is like `_.assign` except that it iterates over own and
  15. * inherited source properties.
  16. *
  17. * **Note:** This method mutates `object`.
  18. *
  19. * @static
  20. * @memberOf _
  21. * @since 4.0.0
  22. * @alias extend
  23. * @category Object
  24. * @param {Object} object The destination object.
  25. * @param {...Object} [sources] The source objects.
  26. * @returns {Object} Returns `object`.
  27. * @see _.assign
  28. * @example
  29. *
  30. * function Foo() {
  31. * this.b = 2;
  32. * }
  33. *
  34. * function Bar() {
  35. * this.d = 4;
  36. * }
  37. *
  38. * Foo.prototype.c = 3;
  39. * Bar.prototype.e = 5;
  40. *
  41. * _.assignIn({ 'a': 1 }, new Foo, new Bar);
  42. * // => { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5 }
  43. */
  44. var assignIn = createAssigner(function(object, source) {
  45. if (nonEnumShadows || isPrototype(source) || isArrayLike(source)) {
  46. copyObject(source, keysIn(source), object);
  47. return;
  48. }
  49. for (var key in source) {
  50. assignValue(object, key, source[key]);
  51. }
  52. });
  53. module.exports = assignIn;