Skip to content

Instantly share code, notes, and snippets.

@flipflop
Created September 29, 2011 15:47
Show Gist options
  • Save flipflop/1251044 to your computer and use it in GitHub Desktop.
Save flipflop/1251044 to your computer and use it in GitHub Desktop.
JavaScript Module
/*
* Module pattern (public and private members within a namespace)
* Creates chassis.example namespace
* Note: ; before parenthesis is deliberate
*
* Below is an example of a flexible multi-part module that can be loaded
* in any order with loose augmentation.
*
*/
;(function ( container, $, doc, undefined ) { // add in more parameters for context e.g. ( container, jQuery, document, undefined )
function createModule() { // Revealing Module Pattern with execution context passed in arguments
var localVar1 = "local variable",
localVar1 = "public variable",
init = function() {
// -------- add pass through JS here (follows execution order of page)
// --------
$(function() {
// add DOM ready init code here
addEvents();
});
}(); // self invoking
function addEvents() {
} // end addEvents()
function display() {
} // end display()
function saveData() {
/* persistence management */
} // end saveData()
function destroy() { // Public method
/* clean up namespace for garbage collection */
chassis.example = null;
} // end destroy()
var contract = { // add public methods / properties here (remove if not needed)
addEvents : addEvents,
destroy : destroy
}
// Public interface (properties and methods)
return contract;
} // end module
// Public API (assigns to my namespace)
container.example = createModule();
})( this.chassis || (this.chassis = {}), jQuery, document ); // end chassis.Example (create namespace and context)
// end chassis.example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment