Created
December 14, 2011 08:03
-
-
Save tastycode/1475684 to your computer and use it in GitHub Desktop.
module pattern example
This file contains hidden or 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
YAHOO.myProject.myModule = function () { | |
//"private" variables: | |
var myPrivateVar = "I can be accessed only from within YAHOO.myProject.myModule."; | |
//"private" method: | |
var myPrivateMethod = function () { | |
YAHOO.log("I can be accessed only from within YAHOO.myProject.myModule"); | |
} | |
return { | |
myPublicProperty: "I'm accessible as YAHOO.myProject.myModule.myPublicProperty.", | |
myPublicMethod: function () { | |
YAHOO.log("I'm accessible as YAHOO.myProject.myModule.myPublicMethod."); | |
//Within myProject, I can access "private" vars and methods: | |
YAHOO.log(myPrivateVar); | |
YAHOO.log(myPrivateMethod()); | |
//The native scope of myPublicMethod is myProject; we can | |
//access public members using "this": | |
YAHOO.log(this.myPublicProperty); | |
} | |
}; | |
}(); // 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