Skip to content

3.02 JS Module Pattern

Ernesto Valdes edited this page Jun 9, 2015 · 4 revisions

REFERENCES

Modules are an integral piece of any robust application's architecture and typically help in keeping the units of code for a project both cleanly separated and organized.

var myNamespace = (function () {
  // 
  var myPrivateVar, myPrivateMethod;
 
  // A private variable
  myPrivateVar = 0;
 
  // A private function
  myPrivateMethod = function( foo ) {
      console.log( foo );
  };
 
  return {
 
    // A public variable
    myPublicVar: "foo",
 
    // A public function utilizing privates
    myPublicFunction: function( bar ) {
 
      // Work with private variable
      myPrivateVar++;
 
      // Call private method 
      myPrivateMethod( bar ); 
    }
  };
 
})();
Clone this wiki locally