passing directly "object literal param"_{ }_ in WeakMap() works , although no reference to it

43 Views Asked by At

I noticed something, when i create an object and set it in WeakMap(), it is stored within WeakMap(), but when i re-assign this variable with value "null", "Grabage collector" remove this key from WeakMap(), because no reference to it.

let object1 = { name: "A" };
const map1 = new WeakMap();
map1.set(object1, "Apple");
console.log(map1.has(object1)); // true
object1 = null; // now garbage collector will remove the object from WeakMap

The Question Here: when i pass an object literal directly as a param in WeakMap(), why this object wouldn't been removed? because it hasn't any reference for it

map1.set({ name: "D" }, "Direct");
//Why map accept this param, although no reference for it

UPDATE: it would been removed as well in case you pass literal object directly to WeakMap

0

There are 0 best solutions below