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.

Ray
  • 4,038
  • 8
  • 36
  • 48

3 Answers3

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

danwellman
  • 9,068
  • 8
  • 60
  • 88
  • 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(); – Ray Jun 07 '12 at 07:56
  • 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 :) – danwellman Jun 07 '12 at 08:04
  • 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? – Ray Jun 07 '12 at 08:11
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...

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • in order to encapsulate private members, refer http://yuiblog.com/blog/2007/06/12/module-pattern/ – Ray Jun 07 '12 at 07:49
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.

Gipsy King
  • 1,549
  • 9
  • 15