Search code examples
javascriptobjectreferencescopeiife

IIFE returned object - reference internal or external object, internally?


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!


Solution

  • 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.