object.assign angular 2 is not working correctlly

I am using object.assign to create new copy of an object upon making any change in the new copy the changes are reflected in the original copy .but i don't want this

 let xyz = Object.assign({}, this.model);

property i changed is deep inside the model.

Is there any other alternative of object.assign ;as any changes in xyz reflects in original object .I know Object.assign works other way .but not sure what's wrong in my model


Shouldn't it just be something like this

let xyz = this.model;
xyz.obj1.prop1 = "Change";

The change will be reflected in both objects.

OR

If you want a copy but not the refernce to the original you can go like this.

let xyz = JSON.parse(JSON.stringify(this.model));
xyz.obj1.prop1 = "Change";

This will change ONLY the property in xyz and NOT in this.model

链接地址: http://www.djcxy.com/p/40714.html

上一篇: 在JavaScript中克隆对象

下一篇: object.assign角度2无法正确工作