I have a function a
where there is a function b
that references foo
in its closure.
class Foo {}
function a() {
const foo = new Foo()
const b = () => {
foo // referencing `foo`
}
b()
};
a() // no memory leaks
after I executed a
, there are no memory leaks for Foo
.
I also tweaked it slightly. Still, no memory leaks for the following examples.
class Foo {}
function a() {
const foo = new Foo()
const b = () => {
if(foo === 'foo') {
}
}
b()
};
a() // no memory leaks
class Foo {}
function a() {
const foo = new Foo()
const b = () => {
console.log(foo.lol)
}
b()
};
a() // no memory leaks
But as soon as I logged out foo
, it causes memory leaks.
class Foo {}
function a() {
const foo = new Foo()
const b = () => {
console.log(foo)
}
b()
};
a() // memory leaks
My question is why console.log(fii)
leaks but referencing foo
in other ways or log out its property console.log(foo.lol)
I tested these in Chrome 103 DevTool console.