ecmascript 6 - Why the obj is being mutated when I am modifying the cloned obj ( using {...}) ? -
lets take object d.
var d = { "e":{ "f": 3 } }
now copying d
t
{...} , assigning new prop.
var t = {...d} t.e._f = 4
why object d being mutated
{ "e": object { "_f": 4, "f": 3 } }
you doing shallow copy. {...d}
equivalent object.assign({}, d)
in turn copies properties one level deep. docs.
for deep cloning, need use other alternatives because object.assign() copies property values. if source value reference object, copies reference value.
so t.e === d.e
referencing same object.
Comments
Post a Comment