The first two lines creates two objects and assign their references to the variables a
and b
:
---------------
a -------------------------------> | {sample: 1} |
---------------
b -------------------------------> | {sample: 9} |
---------------
The line ra = a
assignes whatever reference stored in a
to ra
so now ra
and a
points to the same object, the same goes for rb = b
:
/--------------------------\
| | ---------------
| a ----------------------\-> | {sample: 1} |
| ---------------
| b ------------------------> | {sample: 9} |<-\
| --------------- |
| |
ra -/ |
|
rb -------------------------------------------------/
rc = ra
means that rc
is pointing to whatever ra
is pointing to:
/--------------------------\
| | ---------------
| a ----------------------\-> | {sample: 1} |<----\
| --------------- |
| b ------------------------> | {sample: 9} |<-\ |
| --------------- | |
| | |
ra -/ | |
| |
rb -------------------------------------------------/ |
|
rc ----------------------------------------------------/
ra = b
means that ra
is now pointing to the object pointed to by b
:
---------------
a ------------------------> | {sample: 1} |<----\
--------------- |
b ----------------------/-> | {sample: 9} |<-\ |
| --------------- | |
| | |
ra ----------------------------/ | |
| |
rb -------------------------------------------------/ |
|
rc ----------------------------------------------------/
Now when logging rc
, we find out that rc
is pointing to {sample: 1}
not to {sample: 9}
.
So it's not possible, when assigning a reference of an object to a variable, only that variable changes (point to the object), the other variables that points to the same object remain intact.