Skip to content

Instantly share code, notes, and snippets.

@devdays
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devdays/7024cc84de8550a0e5a7 to your computer and use it in GitHub Desktop.
Save devdays/7024cc84de8550a0e5a7 to your computer and use it in GitHub Desktop.
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