Skip to content

Instantly share code, notes, and snippets.

@m25lazi
Created June 19, 2017 20:13
Show Gist options
  • Save m25lazi/deff3df05b8860cff6a3a87582639f4e to your computer and use it in GitHub Desktop.
Save m25lazi/deff3df05b8860cff6a3a87582639f4e to your computer and use it in GitHub Desktop.
Module pattern in JS
// Taken from Addy Osmani's Essential JS Design Patterns
// https://addyosmani.com/resources/essentialjsdesignpatterns/book/#constructorpatternjavascript
var myNamespace = (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 );
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment