I have a quick question on how garbage collection works in Javascript.
If I have this code:
var c = {name : ‘Bob’}; c = {name : ‘Mary’}; In the code above the variable c is pointing to the object {name : ‘Bob’}. But then I set c to point to another object in memory {name : ‘Mary’}. What will happen to the object ({name : ‘Bob’}) that c was pointing to originally? Will that original object be deallocated in memory since there are no references to it anymore?
In another case:
var c = {name : ‘Bob’}; d = c; c = {name : ‘Mary’}; Now, the original object that c was pointing to ({name : ‘Bob’}) would not be deallocated since d is still pointing to {name : ‘Bob’} even after "c" was changed to point to the new object: {name : ‘Mary’}. Correct?
So basically an object will not be deallocated from memory as long as there are references that are still pointing to it.
Can someone please explain to me if I am thinking about this correctly?