Skip to content

Instantly share code, notes, and snippets.

@jmontross
Created June 21, 2012 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmontross/2967292 to your computer and use it in GitHub Desktop.
Save jmontross/2967292 to your computer and use it in GitHub Desktop.
Javascript module pattern with a closure
var awesomeModule = (function () {
var privateVariable;
function privateFunction() {
//do something;
}
return { // <----- this starts the object being returned when this module function executes
init: function () {
// I have access to privateVariable & privateFunction
} // init
}; // <----- this ends the object being returned when this module function executes
})();
// to Load this guy on dom loaded with prototype
// document.observe('dom:loaded', awesomeModule.init);
// to load this guy with jquery
// $(document).ready(function(){ awesomeModule.init(); })
// more module patterns http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment