You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.5 KiB

4 years ago
  1. # object-assign [![Build Status](https://travis-ci.org/sindresorhus/object-assign.svg?branch=master)](https://travis-ci.org/sindresorhus/object-assign)
  2. > ES2015 [`Object.assign()`](http://www.2ality.com/2014/01/object-assign.html) [ponyfill](https://ponyfill.com)
  3. ## Use the built-in
  4. Node.js 4 and up, as well as every evergreen browser (Chrome, Edge, Firefox, Opera, Safari),
  5. support `Object.assign()` :tada:. If you target only those environments, then by all
  6. means, use `Object.assign()` instead of this package.
  7. ## Install
  8. ```
  9. $ npm install --save object-assign
  10. ```
  11. ## Usage
  12. ```js
  13. const objectAssign = require('object-assign');
  14. objectAssign({foo: 0}, {bar: 1});
  15. //=> {foo: 0, bar: 1}
  16. // multiple sources
  17. objectAssign({foo: 0}, {bar: 1}, {baz: 2});
  18. //=> {foo: 0, bar: 1, baz: 2}
  19. // overwrites equal keys
  20. objectAssign({foo: 0}, {foo: 1}, {foo: 2});
  21. //=> {foo: 2}
  22. // ignores null and undefined sources
  23. objectAssign({foo: 0}, null, {bar: 1}, undefined);
  24. //=> {foo: 0, bar: 1}
  25. ```
  26. ## API
  27. ### objectAssign(target, [source, ...])
  28. Assigns enumerable own properties of `source` objects to the `target` object and returns the `target` object. Additional `source` objects will overwrite previous ones.
  29. ## Resources
  30. - [ES2015 spec - Object.assign](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.assign)
  31. ## Related
  32. - [deep-assign](https://github.com/sindresorhus/deep-assign) - Recursive `Object.assign()`
  33. ## License
  34. MIT © [Sindre Sorhus](https://sindresorhus.com)