0
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?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
jinek
  • 3
  • 2
  • Please ask one question per post only. I've removed the first one as it's an exact duplicate of http://stackoverflow.com/q/3384504/1048572 – Bergi Mar 21 '15 at 19:56
  • You might want to read http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript. You'll need to understand that there are no "members", only object properties and local variables in javascript. And of course you'll need to know [how closures work](http://stackoverflow.com/q/111102/1048572) – Bergi Mar 21 '15 at 20:01
  • sorry about the two part question, however the question in the link you posted is not the same as the one i asked, that question is in regard to the placing of the parentheses while mine was about the why there is a need for the parentheses in the first place. – jinek Mar 21 '15 at 20:06
  • Oh right, sorry for the wrong link. See [here](http://stackoverflow.com/a/15023695/1048572) instead :-) – Bergi Mar 21 '15 at 20:11
  • Oh no problem, that link was just what i've been looking for, thanks a bunch Bergi ! – jinek Mar 21 '15 at 20:18

1 Answers1

1

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Ignacio
  • 688
  • 5
  • 17