JavaScript Module Pattern and Revealing Module pattern - Sergeykp/Shutaf GitHub Wiki

var myModulePatter= (function () {

var myPrivateVar, myPrivateMethod;

// A private counter variable myPrivateVar = 0;

// A private function which logs any arguments myPrivateMethod = function( foo ) { console.log( foo ); };

return {

// A public variable
myPublicVar: "foo",

// A public function utilizing privates
myPublicFunction: function( bar ) {

  // Increment our private counter
  myPrivateVar++;

  // Call our private method using bar
  myPrivateMethod( bar );

}

};

})();

var myRevealingModule = (function () {

    var privateVar = "Ben Cherry",
        publicVar  = "Hey there!";

    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
    };

})();

myRevealingModule.setName( "Paul Kinlan" );