SHOW:
|
|
- or go back to the newest paste.
| 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 | myRevealingModule.setName('Marvin King');
| |
| 75 | ||
| 76 | // ---------------------------------------------------------------------------- | |
| 77 | // ---------------------------------------------------------------------------- | |
| 78 | // ---------------------------------------------------------------------------- | |
| 79 | ||
| 80 | /* | |
| 81 | * The Singleton pattern is thus known because it restricts instantiation | |
| 82 | * of a class to single object. Singletons differ from static classes as we | |
| 83 | * can delay their initialization. generally because they require some information | |
| 84 | * that may not be available during initialization time. For code that is unaware | |
| 85 | * of a previous reference to them, they do not provide a method for easy retrieval. | |
| 86 | * Let's have a look of the structure of singleton: | |
| 87 | */ | |
| 88 | const singletonPattern = (function() {
| |
| 89 | let instance; | |
| 90 | ||
| 91 | function init() {
| |
| 92 | ||
| 93 | // singleton | |
| 94 | function privateMethod() {
| |
| 95 | console.log('privateMethod');
| |
| 96 | } | |
| 97 | ||
| 98 | let privateVariable = 'this is private variable'; | |
| 99 | let privateRandomNumber = Math.random(); | |
| 100 | ||
| 101 | return {
| |
| 102 | publicMethod: function() {
| |
| 103 | console.log('publicMethod');
| |
| 104 | }, | |
| 105 | ||
| 106 | publicProperty: 'this is public property', | |
| 107 | ||
| 108 | getRandomNumber: function() {
| |
| 109 | return privateRandomNumber; | |
| 110 | } | |
| 111 | }; | |
| 112 | } | |
| 113 | ||
| 114 | return {
| |
| 115 | ||
| 116 | // get the singleton instance if one exists | |
| 117 | // or create if it doesn't | |
| 118 | getInstance: function() {
| |
| 119 | if (!instance) {
| |
| 120 | instance = init(); | |
| 121 | } | |
| 122 | return instance; | |
| 123 | } | |
| 124 | }; | |
| 125 | })(); | |
| 126 | ||
| 127 | // usage: | |
| 128 | const single = singletonPattern.getInstance(); | |
| 129 | ||
| 130 | // ---------------------------------------------------------------------------- | |
| 131 | // ---------------------------------------------------------------------------- | |
| 132 | // ---------------------------------------------------------------------------- | |
| 133 | ||
| 134 | /* | |
| 135 | * One of the benefits of using the Prototype pattern is that we've working with | |
| 136 | * the prototype strengths JavaScript has to offer natively rather than attempting | |
| 137 | * to imitate features of other languages. let's look at the pattern example. | |
| 138 | */ | |
| 139 | const myCar = {
| |
| 140 | name: 'bmw', | |
| 141 | drive: function () {
| |
| 142 | console.log('I am driving!');
| |
| 143 | }, | |
| 144 | panic: function () {
| |
| 145 | console.log('wait, how do you stop this thing?');
| |
| 146 | } | |
| 147 | }; | |
| 148 | ||
| 149 | // usage: | |
| 150 | - | // usages: |
| 150 | + | |
| 151 | ||
| 152 | console.log(yourCar.name); // 'bmw' | |
| 153 | - | console.log(yourCar.name); //'bmw' |
| 153 | + | |
| 154 | // ---------------------------------------------------------------------------- | |
| 155 | // ---------------------------------------------------------------------------- | |
| 156 | // ---------------------------------------------------------------------------- | |
| 157 | ||
| 158 | /* | |
| 159 | * Mixins are classes that offer functionality that | |
| 160 | * can be easily inherited by a sub-class or group of | |
| 161 | * sub-classes for the purpose of the function reuse. | |
| 162 | */ | |
| 163 | function Person(firstName, lastName) {
| |
| 164 | this.firstName = firstName; | |
| 165 | this.lastName = lastName; | |
| 166 | this.gender = 'male'; | |
| 167 | ||
| 168 | this.introduce = function introduce() {
| |
| 169 | console.log(this.firstName + ' ' + this.lastName); | |
| 170 | } | |
| 171 | }; | |
| 172 | ||
| 173 | const reporter = new Person('Clark', 'Kent');
| |
| 174 | ||
| 175 | function SuperHero(firstName, lastName, powers) {
| |
| 176 | Person.call(this, firstName, lastName); | |
| 177 | this.powers = powers; | |
| 178 | }; | |
| 179 | ||
| 180 | // SuperHero will inherit Person | |
| 181 | SuperHero.prototype = Object.create(Person.prototype); | |
| 182 | SuperHero.prototype.constructor = SuperHero; | |
| 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 |