Skip to content

Instantly share code, notes, and snippets.

@jesgundy
Created November 28, 2012 20:51
Show Gist options
  • Save jesgundy/4164395 to your computer and use it in GitHub Desktop.
Save jesgundy/4164395 to your computer and use it in GitHub Desktop.
Modular OOP template
window.MyApp = (window.MyApp || {});
MyApp.MyUtils = (function() {
return {
doHandyThing1: function() {},
doHandyThing2: function() {}
};
}());
MyApp.MyModule = (function( utils ) {
var privateValue = "Encapsulated value. No one else can touch this.";
var ModuleExport = function() {
// Do constructor stuff.
utils.doHandyThing1(); // << Take advantage of imported utilities!
};
ModuleExport.prototype = {
publicValue: "Anyone can change this.",
publicMethod: function() {
// Anyone can call this.
},
getEncapsulatedValue: function() {
// Anyone can call this to GET the encapsulated value.
// However, nothing else can SET the encapsulated value.
return privateValue;
}
};
// Export a single instance of our module:
return new ModuleExport();
}( MyApp.MyUtils ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment