Object JavaScript - 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
myModule = function () { | |
//"private" variables: | |
var myPrivateVar = "I can be accessed only from within myModule."; | |
//"private" method: | |
var myPrivateMethod = function () { | |
console.log("I can be accessed only from within myModule"); | |
} | |
myPublicProperty: "I'm accessible as myModule.myPublicProperty.", | |
myPublicMethod: function () { | |
console.log("I'm accessible as myModule.myPublicMethod."); | |
//Within myProject, I can access "private" vars and methods: | |
console.log(myPrivateVar); | |
console.log(myPrivateMethod()); | |
//The native scope of myPublicMethod is myProject; we can | |
//access public members using "this": | |
console.log(this.myPublicProperty); | |
} | |
return { | |
// This section is what makes the property and method | |
// publicly available | |
myPublicProperty: myPublicProperty; | |
myPublicMethod: myPublicMethod; | |
}; | |
}(); // the parens here cause the anonymous function to execute and return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment