I read this of the book "Professional Javascript for Web Developers" but I cannot comprehend the explanation provided in the book.
let outer = function() {
let name = 'Jake';
return function() {
return name;
};
};
This leaks the memory allocated for name. This code creates an internal closure, so as long as the outer function exists, the name variable cannot be cleaned up because there will be a persistent reference to it through that closure. If the contents of the name variable were extremely large instead of just a short string, major problems could result.
I'm don't know if this variable will be passed by a copy or a reference, but from my understanding:
- Why would the name variable not freed, if it passes a copy to the return headless function?
- Why would it be a memory leak if it passes a reference and is being used?
(I'm new to JS, my question might seem silly but it really confuses me and please help)