They are the exact same thing (as @Pointy pointed out, they are the same thing until someone changes external
on the outside), but it just feels weird to use external
here, and it is not pretty clear because you are using it inside its own definition sort of.
IIFE returned object - reference internal or external object, internally?
-
05-08-2022 - |
题
Is there any practical difference between using the internal or external object reference when creating an object using immediately invoked function expression? Example:
var external = (function(){
var internal = {};
internal.a = function(){
//... do stuff
}
internal.b = function(){
internal.a();
// ^ OR v
external.a();
}
return internal;
})();
Both work. The only difference I could think of is possibly internal
is cleaned up after being invoked if there's no direct calls to it, but that may be moot since the same object lives in external
. If there's a duplicate, forgive me - I was unable to articulate what I wanted to know well enough for a search engine to give me a clear answer.
Thanks!
解决方案
其他提示
One interesting difference is that the reference to external
will mean that the lexical scope that contains the external var
declaration will be pinned from inside the object. That could be interpreted as a memory leak, depending on what happens to the object.