Using Object.assignin your example does not do any good.
Object.assigncreates a shallow copy. Basically, use Object.assignwill create a new instance of the object, preserving the original object. In terminology, Reactall this concerns the preservation of an immutable object.
var obj1 = {prop1: "A"};
var obj2 = Object.assign({}, obj1);
obj1 === obj2;
This can be achieved:
var obj2 = {
prop1: obj1.prop1
};
obj1 === obj2;
, Object.assign (string, number, boolean ), ( ). Object.assign , Array, Function, Object, null, undefined.
:
var obj3 = {
fn: function() {}
};
var obj4 = Object.assign({}, obj3);
obj3.fn === obj4.fn;
Object.assign .
. Mozilla: https://developer.mozilla.org/en-US/docs/Glossary/Primitive