Does the interpreter provide a hidden reference to the function in the background, or does the interpreter somehow embed the function?
Unable to respond globally for all javascript engines. But the parser will most likely need to reference your function a for the anonymous functions that you define on document.onclick and document.onkeyup . Thus, a not immediately destroyed, it is not available only for javascript scope constraints.
This is why faults can lead to memory leaks, because it is really wise for a motor to play a . (which was a problem with the IE engine and is still not handled carefully). And since you cannot dereference a manually because you do not have access to the variable, quick fixes for memory leaks arising from this type of pattern do not exist.
Your example is also not closure , because it does not return a function, but deals with the same kind of problems with a variable scope, because it refers to the local variable a inside functions that do not use the same scope ( document.onclick and document.onkeyup ) .
I very much suspect that not a single engine will try to inline function a , as this unnecessarily duplicates the function object. Most likely, it is stored with pointer , and the specified reference counter can be easily destroyed as soon as the reference counter is 0 (in your example, when document.onclick and document.onkeyup dereferenced (set to null or undefined ).
Is this type of code to avoid an effective global variable?
Avoiding global variables is always helpful, so overall: yes, this is an effective way to avoid global variables. Every good engine must also destroy a once document.onclick and document.onkeyup dereferenced.
source share