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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -