Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@robatron
Last active December 19, 2015 07:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robatron/5919951 to your computer and use it in GitHub Desktop.
Save robatron/5919951 to your computer and use it in GitHub Desktop.
JavaScript Module Pattern Example with "Passive Attachment"
/** JavaScript Module Pattern Example with "Passive Attachment"
*/
(function(
win,
doc,
$,
a,
b
){
// "Passively attach" your new module to global through your namespace,
// i.e., attach only if your module name doesn't already exist to avoid
// clobbering existing modules with the same name
my.namespace.c = my.namespace.c || function(){
var self = this;
// Create "private" module properties
var privPropA = ...;
var privPropB = function(){...};
// Create "public" module properties
self.pubPropA = ...;
self.pubPropB = function(){...};
// Use "imported" modules
$(doc).ready(function(){...})
a();
b();
return self;
};
})(
window,
document,
jQuery,
my.namespace.a,
my.namespace.b
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment