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
ES6 Object.assign()
ponyfill
I would love for this to be a fully compliant polyfill, but I have no idea how to read the ES6 spec. Help appreciated :)
Ponyfill: An almost conforming polyfill which doesn't overwrite the native method
Install
Download manually or with a package-manager.
npm install --save object-assign
bower install --save object-assign
component install sindresorhus/object-assign
Example
objectAssign({foo: 0}, {bar: 1});
[{bar: 1}, {baz: 2}].reduce(objectAssign, {foo: 0});
API
objectAssign(target, source)
Assigns enumerable own properties of the source
object to the target
object and returns the target
object.
License
MIT © Sindre Sorhus