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!

有帮助吗?

解决方案

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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top