In the following code
a = 1
b = {"a": a} // this output {"a": 1}
a = 2
console.log(b) // this still output {"a": 1}
In such situation, will the memory which store the content 1
be free when reassign the variable to 2?
In the following code
a = 1
b = {"a": a} // this output {"a": 1}
a = 2
console.log(b) // this still output {"a": 1}
In such situation, will the memory which store the content 1
be free when reassign the variable to 2?
variable did not lose its memory space until it destroyed.
when you write
a = 1
b = {"a": a}
value of a is assigned to a node of object b. a variable is not bind to a node. if you want to update node a to new value you can do by:
b.a = 2;
console.log(b.a); //will print 2
b = {"a": a} // this output {"a": 1}
this instruction tells the compiler to create a new object and set the key "a" to the same value the variable a has right now, so the value 1 is copied into the memory of b.