The Revealing Module Pattern
The Revealing Module Pattern is a variant of the Module Pattern with a known and widely recognized name. What makes this pattern special is that it combines the best parts of the Object Literal Pattern and the Module Pattern. All the members of the Module are declared inside an IIFE, which at the end, returns an Object Literal containing only the public members of the Module and is assigned to the variable that acts as our Namespace:
var simpleguid = (function() { var guid = 1; function init() { guid = 1; } function increaseCounter() { guid++; } function getNext() { var nextGuid = guid; increaseCounter(); return nextGuid; } return { init: init, getNext: getNext }; })();
One of the main benefits of this pattern that differentiates it from other variants is that it allows us to write all the code of our Module inside the IIFE, just like we would if they would be declared on the Global Namespace...