Guest User

Untitled

a guest
Dec 17th, 2021
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Modules are an integral piece of any robust
  3.  * application's architecture and typically
  4.  * help in keeping the unit of code for a
  5.  * project cleanly separated and organized.
  6.  *
  7.  * There are several option for implementing modules. These include:
  8.  * - Object literal notation
  9.  * - The modules Pattern
  10.  * - AMD modules
  11.  * - CommonJS module
  12.  * - ECMAScript Harmony modules
  13.  */
  14.  
  15. // Object literal
  16. const newObject = {
  17.     variableKey: variableValue,
  18.     functionKey: function () {
  19.         // …
  20.     }
  21. };
  22.  
  23. // ----------------------------------------------------------------------------
  24. // Let's begin looking at an implementation of the Module pattern
  25. // by created a module that is self-contained.
  26.  
  27. // Modules Pattern
  28. const testModule = (function () {
  29.     let counter = 0;
  30.     return {
  31.         incrementCounter: function () {
  32.             return ++counter;
  33.         },
  34.         resetCounter: function () {
  35.             counter = 0;
  36.         }
  37.     };
  38. })();
  39.  
  40. // usage:
  41. testModule.incrementCounter();
  42. testModule.resetCounter();
  43.  
  44. // ----------------------------------------------------------------------------
  45. // One thing that the revealing module can do is avoiding
  46. // repeat the name of the main object when we want to call
  47. // one public method from another or access public variables.
  48.  
  49. // Revealing Module Pattern
  50. const myRevealingModule = (function () {
  51.     let privateVariable = 'not okay',
  52.         publicVariable = 'okay';
  53.  
  54.     function privateFun() {
  55.         return privateVariable;
  56.     }
  57.  
  58.     function publicSetName(strName) {
  59.         privateVariable = strName;
  60.     }
  61.  
  62.     function publicGetName() {
  63.         privateFun();
  64.     }
  65.  
  66.     return {
  67.         setName: publicSetName,
  68.         message: publicVariable,
  69.         getName: publicGetName
  70.     };
  71. })();
  72.  
  73. // usage:
  74.  
  75. myRevealingModule.setName('Marvin King');
  76.  
  77. // ----------------------------------------------------------------------------
  78. // ----------------------------------------------------------------------------
  79. // ----------------------------------------------------------------------------
  80.  
  81. /*
  82.  * The Singleton pattern is thus known because it restricts instantiation
  83.  * of a class to single object. Singletons differ from static classes as we
  84.  * can delay their initialization. generally because they require some information
  85.  * that may not be available during initialization time. For code that is unaware
  86.  * of a previous reference to them, they do not provide a method for easy retrieval.
  87.  * Let's have a look of the structure of singleton:
  88.  */
  89. const singletonPattern = (function() {
  90.     let instance;
  91.  
  92.     function init() {
  93.  
  94.         // singleton
  95.         function privateMethod() {
  96.             console.log('privateMethod');
  97.         }
  98.  
  99.         let privateVariable = 'this is private variable';
  100.         let privateRandomNumber = Math.random();
  101.  
  102.         return {
  103.             publicMethod: function() {
  104.                 console.log('publicMethod');
  105.             },
  106.  
  107.             publicProperty: 'this is public property',
  108.  
  109.             getRandomNumber: function() {
  110.                 return privateRandomNumber;
  111.             }
  112.         };
  113.     }
  114.  
  115.     return {
  116.  
  117.         // get the singleton instance if one exists
  118.         // or create if it doesn't
  119.         getInstance: function() {
  120.             if (!instance) {
  121.                 instance = init();
  122.             }
  123.             return instance;
  124.         }
  125.     };
  126. })();
  127.  
  128. // usage:
  129. const single = singletonPattern.getInstance();
  130.  
  131. // ----------------------------------------------------------------------------
  132. // ----------------------------------------------------------------------------
  133. // ----------------------------------------------------------------------------
  134.  
  135. /*
  136.  * One of the benefits of using the Prototype pattern is that we've working with
  137.  * the prototype strengths JavaScript has to offer natively rather than attempting
  138.  * to imitate features of other languages. let's look at the pattern example.
  139.  */
  140. const myCar = {
  141.     name: 'bmw',
  142.     drive: function () {
  143.         console.log('I am driving!');
  144.     },
  145.     panic: function () {
  146.         console.log('wait, how do you stop this thing?');
  147.     }
  148. };
  149.  
  150. // usages:
  151. const yourCar = Object.create(myCar);
  152.  
  153. console.log(yourCar.name); //'bmw'
  154.  
  155. // ----------------------------------------------------------------------------
  156. // ----------------------------------------------------------------------------
  157. // ----------------------------------------------------------------------------
  158.  
  159. /*
  160.  * Mixins are classes that offer functionality that
  161.  * can be easily inherited by a sub-class or group of
  162.  * sub-classes for the purpose of the function reuse.
  163.  */
  164. function Person(firstName, lastName) {
  165.     this.firstName = firstName;
  166.     this.lastName = lastName;
  167.     this.gender = 'male';
  168.  
  169.     this.introduce = function introduce() {
  170.         console.log(this.firstName + ' ' + this.lastName);
  171.     }
  172. };
  173.  
  174. const reporter = new Person('Clark', 'Kent');
  175.  
  176. function SuperHero(firstName, lastName, powers) {
  177.     Person.call(this, firstName, lastName);
  178.     this.powers = powers;
  179. };
  180.  
  181. // SuperHero will inherit Person
  182. SuperHero.prototype = Object.create(Person.prototype);
  183. const superman = new SuperHero('Clark', 'Kent', ['flight', 'heat-vision']);
  184.  
  185. console.log(reporter);
  186. console.log(superman); // output personal attributes as well as power
  187.  
Advertisement
Add Comment
Please, Sign In to add comment