Created
November 6, 2014 09:55
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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