Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** How to write a JavaScript module: */
  2.  
  3. var myModule = (function() {
  4.         // exports contains "public" methods and properties:
  5.     var exports = {},
  6.         // priv contains private functions and data
  7.         priv = {};
  8.    
  9.     // Optional init function. See 3rd last line.
  10.     priv.init = function() {
  11.         // here you might choose to set up some stuff, get data, etc.
  12.     };
  13.    
  14.     // this is how you would make a private function. It's only visible inside the module.
  15.     priv.doSomething = function() {
  16.         priv.someValue = Math.random();
  17.     };
  18.    
  19.     exports.whoAreYou = function() {
  20.         return "I'm just a module. What's it to you?";
  21.     };
  22.    
  23.     // OPTIONAL:  Creating a private "init" function is often useful.
  24.     priv.init();
  25.     return exports;
  26. }());
  27.  
  28.  
  29.  
  30.  
  31. /** How to interact with a module: */
  32.  
  33. // assume we have a module "myModule", as defined above.
  34.  
  35. console.log( myModule );        // look at your console to see the module's public stuff.
  36.  
  37. console.log( myModule.whoAreYou() );    // logs that stuff from above.
  38.  
  39. console.log( myModule.doSomething() );      // ERROR: doSomething is private!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement