Skip to content

Instantly share code, notes, and snippets.

@makenova
Created July 1, 2015 21:35
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 makenova/ab2ceb0e280bd188171e to your computer and use it in GitHub Desktop.
Save makenova/ab2ceb0e280bd188171e to your computer and use it in GitHub Desktop.
JS module pattern
function evilCorp (name, laugh){
this.name = name;
this.laugh = laugh;
}
evilCorp.prototype.takeover = function(){
return "My name is " + this.name + " and I am your new master! " + this.laugh;
}
var $ = new evilCorp('condescendingSeniorDev', 'muwahaha');
var myNamespace = (function(niceguy){
var exports = {};
// innocuous will not clutter the global namespace
exports.innocuous = function foo(){
return niceguy.takeover();
}
return exports;
}($)) // globals can be passed in and 'renamed' within module closure
console.log(myNamespace.innocuous());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment