hi i have a query and was unable to get exact answer. I have a below closure function
function closure() {let innerVal; return function() {innerVal = innerVal+10}}
let inst = closure();
inst();
console.log('end');
will innerVal
be cleared during GC after inst()
is called or not?
2
Answers
I created a little demo using WeakRef
You can run it and wait 100 seconds
It prints
undefined
in the end so the object has been garbage collectedIt won’t be garbage collected until after the invocation. However, no garbage collection algorithm is specified in the ECMAScript language specification. So…
…depends on the specific runtime implementation: you will need to consult the documentation/source code of the runtime in question to obtain that information. You can learn more about garbage collection observability at the Memory management article on MDN.