Created
January 10, 2012 20:09
-
-
Save hunterloftis/1590914 to your computer and use it in GitHub Desktop.
Simple module pattern versatility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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