Reputation: 193
Say I have an object
let a = {
b: {
name: "Sugarcane"
}
}
and I do
let c = a.b
and I later nullify the a
object like so:
a = null
does the reference to b
object by c
prevent the previous object stored in a
from being garbage collected in Javascript?
Upvotes: 1
Views: 220
Reputation: 385
Actually no
, as long as there is a reference to the object stored in memory, the GC will not be able to free this object from memory.
When you nullify object A
it is removed from memory, but this does not affect its properties if they are objects and are stored in another variable.
You are just changing the reference from A
to NULL
, but B
will continue to exist in memory as long as C
continues to have its reference stored.
B
will only be removed from memory when C
is nullified.
let a = {
b: {
name: "Sugarcane"
}
};
let c = a.b;
a = null;
console.log(
"A value: ", a,
"\nC value:", c,
"\nC.name value:", c.name
);
Upvotes: 1