9

In the module pattern in JavaScript "Immediately-Invoked Function Expressions" (also known as self-executing anonymous functions) are used as self executing functions that return an object. How can a self-executing function hide private variables and only expose the returned object. Why does this not happen with a normal JavaScript function? So in the following mini module, why could we not achieve the same concept of encapsulation without the enclosing ()()?

var Module = (function () {
    var privateVariable = "foo",
        privateMethod = function () {
            alert('private method');
        };

    return {
        PublicMethod: function () {
            alert(privateVariable); 
            privateMethod(); 
        }
    };
})();
1
  • Getting your mind around what is assigned should give you 50% of the necessary understanding. The other 50% comes from an understanding of closures. If you still have trouble then read Douglas Crockford's "Private Members in JavaScript". Commented May 5, 2013 at 14:29

3 Answers 3

9

How can a self-executing function hide private variables and only expose the returned object. Why does this not happen with a normal JavaScript function?

It does happen with normal JavaScript functions.

function MakeModule() {
    var privateVariable = "foo",
        privateMethod = function () {
            alert('private method');
        };

    return {
        PublicMethod: function () {
            alert(privateVariable); 
            privateMethod(); 
        }
    };
}

var Module = MakeModule();

would work just fine.

The only difference is that the anonymous function introduces one less global variable and allows for itself to be garbage collected while MakeModule can't be collected unless explicitly deleted by the author.

Sign up to request clarification or add additional context in comments.

2 Comments

Can the IIFE really be GC'd if it formed a closure that contained things that were referenced by whatever it returned?
@DaggNabbit, yes. As long as its arguments object is not used. The function object is created as an operand to the call, so as soon as the call completes, the initial reference to it is no longer used and the only thing left from the call is any execution context that store variables closed over by escaping inner functions.
2

The privateness is because of closures. The "var privateVariable" is closed over by "PublicMethod", so only that function can access the variable because only it has it in its closure. It cannot be referenced by anything else and is "private"

This happens not only in "Immediately-Invoked Function Expressions" but also in normal function calls. It is just a way to immediately create the closure when defining the module instead of doing it later when you call the outer function.

Also see this post from Douglas Crockford himself: http://javascript.crockford.com/private.html

1 Comment

The idea of "immediately creating the closure" best answers my question I think.
0

You can define a anonymous function via named function.

Example:

//factorial
(function(n){
    var self = function(n){
        //call self
        return n > 0 ? (self(n-1) * n) : 1;
    }
    return self;
})()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.