Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Another example of the module pattern implementation in JS
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