Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.