Created
February 12, 2015 20:51
-
-
Save darbula/89307a26d446847675a3 to your computer and use it in GitHub Desktop.
Revealing module pattern
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
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