Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** How to write a JavaScript module: */
- var myModule = (function() {
- // exports contains "public" methods and properties:
- var exports = {},
- // priv contains private functions and data
- priv = {};
- // Optional init function. See 3rd last line.
- priv.init = function() {
- // here you might choose to set up some stuff, get data, etc.
- };
- // this is how you would make a private function. It's only visible inside the module.
- priv.doSomething = function() {
- priv.someValue = Math.random();
- };
- exports.whoAreYou = function() {
- return "I'm just a module. What's it to you?";
- };
- // OPTIONAL: Creating a private "init" function is often useful.
- priv.init();
- return exports;
- }());
- /** How to interact with a module: */
- // assume we have a module "myModule", as defined above.
- console.log( myModule ); // look at your console to see the module's public stuff.
- console.log( myModule.whoAreYou() ); // logs that stuff from above.
- console.log( myModule.doSomething() ); // ERROR: doSomething is private!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement