Skip to content

Instantly share code, notes, and snippets.

@devdays
Last active August 29, 2015 14:10
Embed
What would you like to do?
Object JavaScript - Revealing Module Pattern
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