Skip to content

Instantly share code, notes, and snippets.

@hunterloftis
Created January 10, 2012 20:09
Show Gist options
  • Save hunterloftis/1590914 to your computer and use it in GitHub Desktop.
Save hunterloftis/1590914 to your computer and use it in GitHub Desktop.
Simple module pattern versatility
(function(exports, $, otherLibrariesYouNeed) {
var localVariable = 'This is bound in the closure, so I will not create accidental globals.';
function PrivateFunction() {
}
function MyConstructor() {
this.property = 'something';
}
MyContructor.prototype = {
method1: function() { /* can use PrivateFunction() */ },
method2: function() { /* ... */ }
};
// Now you can either export a singleton:
exports.mySingleton = new MyConstructor();
// Or you can export a constructor (kind of like a Class)
exports.MyConstructor = MyConstructor;
})(window, jQuery, otherStuff);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment