Why are the returned data the same?
While setting statObj.chatObj.xAxis
, xAxis
is not found on chatObj
, so xAxis
is searched on the prototype chain (statList
is the prototype of chatObj
), where we could find it there. Until now we are done with the part statObj.chatObj.xAxis
, next we move to create a .data
key which will be create on statList.xAxis
. Assignments with both statObj.carObj.xAxis.data
and statObj.saObj.xAxis.data
oberride what was set by statObj.chatObj.xAxis.data
on statList.xAxis.data
, that is why we have the result of statObj.XX.xAxis.data
the last set value which is [7, 8, 9]
.
Usually I do solve this by using the following trick
statObj.chatObj = JSON.parse(JSON.stringify(statList));
I do not say this is a good practice, but in this way I am making sure the created object has it's own adress in the memory.
The problem with Object.assign()
is that it is not working with nested properties, anyway the created object's are mutable.