![]() |
VOOZH | about |
The Object.assign() method is used to copy the values and properties from one or more source objects to a target object. It invokes getters and setters since it uses both [[Get]] on the source and [[Set]] on the target.
Object.assign(target, ...sources);Object.assign() returns the target object.
Example 1: In this example, the properties of the object "obj1" i.e. { a: 10 } is copied to the target object "new_obj".
Example 2: In this example, the properties of three source objects "obj1, obj2, obj3" are copied to the target object "new_obj". The value of any pre-existing key-value pair that existed in the previous object will be over-written. For example, obj1.b which has a value of 10 will now be overwritten with obj2.b which has a value of 20
Example 3: In this example, the properties of three source objects "obj1, obj2, obj3" are copied to the target object "new_obj" and the target object gets the overwritten values.
In the above code the properties are overwritten by other objects that have the same properties later in the same order of parameters.
We have a complete list of JavaScript Object methods, to check those please go through this JavaScript Object Complete Reference article.