Skip to content

Instantly share code, notes, and snippets.

@darbula
Created February 12, 2015 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save darbula/89307a26d446847675a3 to your computer and use it in GitHub Desktop.
Save darbula/89307a26d446847675a3 to your computer and use it in GitHub Desktop.
Revealing module pattern
if (typeof DOJ == 'undefined') {
DOJ = {};
}
// http://toddmotto.com/mastering-the-module-pattern/
DOJ = function () {
var _myPrivateVar = "_myPrivateVar can be accessed only from within DOJ.";
var myPublicProperty = "myPublicProperty is accessible as DOJ.myPublicProperty.";
var _myPrivateMethod = function () {
console.log("_myPrivateMethod can be accessed only from within DOJ.");
return "Returned from _myPrivateMethod";
};
var myPublicMethod = function () {
console.log("myPublicMethod is accessible as DOJ.myPublicMethod.");
//Within myProject, I can access "private" vars and methods:
console.log(_myPrivateVar);
console.log(_myPrivateMethod());
//The native scope of myPublicMethod is DOJ; we can
//access public members using "this":
console.log(this.myPublicProperty);
};
// return public properties and methods
return {
myPublicProperty: myPublicProperty,
myPublicMethod: myPublicMethod
};
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment