Skip to main content
added 234 characters in body
Source Link
theonly1me
  • 1.2k
  • 7
  • 15

That is because in the below line, you are not changing the copied property of src, instead you are re-assigning both 'key' and 'value' and replacing the old property altogether with a new property.

dst[0]={name:"tom"} 

is the same as doing this:

dst[0]={newName:"tom"} 

It's considered as dst's new property instead of the one copied from src, and that is the reason the original object does not change, because this 'name' property is not the same one that was copied from the original object.

When you use the {name : "tom" } to re-assign the value to dst, then dst[0]'s memory is released from the heap and a new object replaces dst[0] altogether, it is not the same object that was shallow copied from src[0].

That is because in the below line, you are not changing the copied property of src, instead you are re-assigning both 'key' and 'value' and replacing the old property altogether with a new property.

dst[0]={name:"tom"} 

is the same as doing this:

dst[0]={newName:"tom"} 

It's considered as dst's new property instead of the one copied from src, and that is the reason the original object does not change, because this 'name' property is not the same one that was copied from the original object.

That is because in the below line, you are not changing the copied property of src, instead you are re-assigning both 'key' and 'value' and replacing the old property altogether with a new property.

dst[0]={name:"tom"} 

is the same as doing this:

dst[0]={newName:"tom"} 

It's considered as dst's new property instead of the one copied from src, and that is the reason the original object does not change, because this 'name' property is not the same one that was copied from the original object.

When you use the {name : "tom" } to re-assign the value to dst, then dst[0]'s memory is released from the heap and a new object replaces dst[0] altogether, it is not the same object that was shallow copied from src[0].

Source Link
theonly1me
  • 1.2k
  • 7
  • 15

That is because in the below line, you are not changing the copied property of src, instead you are re-assigning both 'key' and 'value' and replacing the old property altogether with a new property.

dst[0]={name:"tom"} 

is the same as doing this:

dst[0]={newName:"tom"} 

It's considered as dst's new property instead of the one copied from src, and that is the reason the original object does not change, because this 'name' property is not the same one that was copied from the original object.