On Github westmonroe / modern-web-training
West Monroe Partners New Hire Training
An unnamed (anonymous) function that protects all contents.
(function(){ var foo = true; function doSomething(){...} function doSomethingElse(){...} function doAnotherThing(){...} })(); var foo = false;
A named function that returns some variables/functions (public) and hides others (private).
myNameSpace = (function(){ var foo = null; function doSomething(){...} function doSomethingElse(){...} function doAnotherThing(){...} return { doX:doSomething, doY:doSomethingElse } })(); // This works. myNameSpace.doX(); // This does not work. myNameSpace.doSomething();