0

I am just wondering why self execution code should be needed when implement module pattern in javascript.

Following code is typical module pattern sample :

    var app = app || {};
    app.model = app.model || {};

    app.model.person = (function () {
        var say = function () {
            alert('say');
        };

        return {
            saySomething: say
        }
    })();

But, I cannot find out the reason why this function should be self executed with closing curly brace.

3 Answers 3

2

The function/module isn't self-executed with the closing curly brace, it's self-executed (or self-invoked) with the () at the end.

The reason for doing this is to emulate privacy in JavaScript. The function is executed immediately and the saySomething function is returned, which remains available after the return because of a closure, and is therefore public. The say function is not returned and is not accessible outside of the function so is therefore private

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

3 Comments

I am still wondering why self execution is recommended. If I make person to not anonymous function but normal constructor function, what is different? In case of normal constructor function I may call saySomething function like this : var p = new app.model.person(); p.saySomething();
What's different is that the saySomething() function is accessible everywhere, a public member. self-execution isn't always recommended, it's just useful in certain situations, such as when you would like to emulate privacy in JavaScript. If you're not concerned with private members and public members there's no reason why it should be used. There are many patterns used in JavaScript to achieve different things, the module pattern is just one of them :)
privacy,,, is can be achieved even if it is not a anonymous and self execution. Code like "app.model.person = function(){ ... }; var p = new app.model.person() " has same result regarding encapsulation, isn't it?
1

Who says it needs to be implemented this way? I see no reason why much more readable:

var app = app || {};
app.model = app.model || {};

app.model.person = {
    saySomething: function () {
        alert('say');
    }
}

can't be used...

1 Comment

in order to encapsulate private members, refer yuiblog.com/blog/2007/06/12/module-pattern
1

In your example there is really no point, because the say function is later exposed in the returned object. But consider this:

app.model.person = (function () {
    var privateVar = 'I am private';

    var say = function () {
        alert(privateVar);
    };

    var doSomething = function() {
        // operates on privateVar
    };

    return {
        saySomething: say
    }
})();

Here it makes sense, because privateVar is never accessible from outside of the object.

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.