Skip to content

Instantly share code, notes, and snippets.

@Stoffo
Created November 6, 2014 09:55
Show Gist options
  • Save Stoffo/46ab999b9a18e00ff0cc to your computer and use it in GitHub Desktop.
Save Stoffo/46ab999b9a18e00ff0cc to your computer and use it in GitHub Desktop.
Another example of the module pattern that exposes the module a little differently and makes use of a shared private cache. This method encourages more of an object creation approach where we can optimize performance by being efficient with shared storage.
var MyModule = ( function( window, undefined ) {
// this object is used to store private variables and methods across multiple instantiations
var privates = {};
function MyModule() {
this.myMethod = function myMethod() {
alert( 'my method' );
};
this.myOtherMethod = function myOtherMethod() {
alert( 'my other method' );
};
}
return MyModule;
} )( window );
// example usage
var myMod = new MyModule();
myMod.myMethod(); // alerts "my method"
myMod.myOtherMethod(); // alerts "my other method"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment