jinek
jinek

Reputation: 3

Private members in the revealing module pattern

var app = (function(){ 
    var foo = 'x';

    var bar = function (){
        ...
    };

    var xx = function () {
        bar();
    }

     return {
        xx:xx
     }

})();

since the function is a Immediately-Invoked Function Expression (IIFE) the app var is assigned the returning object literal. But in what way are the private members returned? Is the member foo not present in app since its not referenced in any public method? How is the reference to bar stored in the app variable?

Upvotes: 0

Views: 81

Answers (1)

Ignacio
Ignacio

Reputation: 688

The variable foo is private because it cannot be accesed from outside the IIFE, but it can from xx, bar, and the other parts of the IIFE as it is within (or above) their scope. The app variable will only know about the object {xx: xx}, and nothing more, so the IFFE acts like a black box. The app variabl and the adjacent ones know what gets out of it but it can't get any value of the inside, e.g. of foo.

Upvotes: 1

Related Questions