IFFE

We want to create an IFFI so we can immediately invoke it so it does not have a name.

//Create a function without a name, this won't work as it thinks its a function declaration: 
function() {
}

//But if we do this, we have a function expression:
var a = function() {
}

//So we have to trick the compiler into thinking this is a function expression :
function() {
}

//How to do that? Wrap it in parenthesis:
(function() {
});

To invoke that function, with () at the end: 
(function() {
   function privateFunction() {
    console.log("private"); -> Cannot be accessed outside that function
   }
   window.public2 = function() { -> Making that public to the global object
    privateFunction() -> Can still be access
    console.log("public"); 
   }
})();

results matching ""

    No results matching ""