What is object-assign?
The object-assign npm package is a polyfill for the Object.assign() method, which is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object with properties and values copied from the source objects.
What are object-assign's main functionalities?
Copying properties
This feature allows you to copy the properties from one or more source objects to a target object. The target object in this example ends up being { a: 1, b: 2 }.
var target = { a: 1 }; var source = { b: 2 }; var returnedTarget = objectAssign(target, source);
Merging multiple sources
object-assign can merge properties from multiple source objects into a single target object. The target object in this example ends up being { a: 1, b: 2, c: 3 }.
var target = { a: 1 }; var source1 = { b: 2 }; var source2 = { c: 3 }; var returnedTarget = objectAssign(target, source1, source2);
Overwriting properties
If the source and target object have the same key, the property in the target object will be overwritten by the property from the source object. The target object in this example ends up being { a: 1, b: 2, c: 2 }.
var target = { a: 1, b: 1 }; var source = { b: 2, c: 2 }; var returnedTarget = objectAssign(target, source);
Other packages similar to object-assign
extend
The 'extend' package is similar to object-assign in that it is used to copy properties from one or more source objects to a target object. However, 'extend' can perform a deep copy where nested objects and arrays are also recursively copied, unlike object-assign which only performs a shallow copy.
lodash.assign
lodash.assign is a method from the Lodash library that offers similar functionality to object-assign. It copies own enumerable properties from source objects to a target object. Lodash provides a more extensive set of utilities for working with objects, arrays, and other types, and lodash.assign is part of this larger toolkit.
deep-assign
deep-assign is an npm package that also copies properties from source objects to a target object, similar to object-assign. The key difference is that deep-assign supports deep copying, meaning that it can copy properties at all levels of object nesting, not just the top level.
object-assign
ES2015 Object.assign()
ponyfill
Use the built-in
Node.js 4 and up, as well as every evergreen browser (Chrome, Edge, Firefox, Opera, Safari),
support Object.assign()
:tada:. If you target only those environments, then by all
means, use Object.assign()
instead of this package.
Install
$ npm install --save object-assign
Usage
const objectAssign = require('object-assign');
objectAssign({foo: 0}, {bar: 1});
objectAssign({foo: 0}, {bar: 1}, {baz: 2});
objectAssign({foo: 0}, {foo: 1}, {foo: 2});
objectAssign({foo: 0}, null, {bar: 1}, undefined);
API
objectAssign(target, [source, ...])
Assigns enumerable own properties of source
objects to the target
object and returns the target
object. Additional source
objects will overwrite previous ones.
Resources
Related
License
MIT © Sindre Sorhus