Javascript: Revealing Module Pattern
An example of how to use the Revealing Module Pattern:
var RevealingModuleExample = (function () {
var privateVar = "Jane Doe",
publicVar = "Hello World!";
function privateFunction() {
console.log("Name:" + privateVar);
}
function publicSetName(strName) {
privateVar = strName;
}
function publicGetName() {
privateFunction();
}
// Reveal public pointers to private functions and properties
return {
setName: publicSetName,
greeting: publicVar,
getName: publicGetName
};
})();
RevealingModuleExample.setName( "John Doe" );
The pattern can also be used to reveal private functions and properties with a more specific naming scheme:
var RevealingModuleExample = (function () {
var privateCounter = 0;
function privateFunction() {
privateCounter++;
}
function publicFunction() {
publicIncrement();
}
function publicIncrement() {
privateFunction();
}
function publicGetCount(){
return privateCounter;
}
// Reveal public pointers to private functions and properties
return {
start: publicFunction,
increment: publicIncrement,
count: publicGetCount
};
})();
RevealingModuleExample.start();
Source: https://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript (Author: Addy Osmani)