|
| 1 | +/** |
| 2 | + * Object literals |
| 3 | + */ |
| 4 | + |
| 5 | +// Here is an example of module using object literal syntax: |
| 6 | + |
| 7 | +let myModule = { |
| 8 | + myProperty: "someValue", |
| 9 | + myConfig: { |
| 10 | + useCaching: true, |
| 11 | + language: "en", |
| 12 | + }, |
| 13 | + myMethod1: function () { |
| 14 | + console.log("I can haz functionality?"); |
| 15 | + }, |
| 16 | + myMethod2: function () { |
| 17 | + console.log("Caching: ", this.myConfig.useCaching ? "enabled" : "disabled"); |
| 18 | + }, |
| 19 | + // override the current configuration |
| 20 | + myMethod3: function (newConfig) { |
| 21 | + if (typeof newConfig === "object") { |
| 22 | + this.myConfig = newConfig; |
| 23 | + console.log(this.myConfig.language); |
| 24 | + } |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +myModule.myMethod1(); |
| 29 | +myModule.myMethod2(); |
| 30 | +myModule.myMethod3({ |
| 31 | + useCaching: false, |
| 32 | + language: "vi", |
| 33 | +}); |
| 34 | +myModule.myMethod2(); |
| 35 | + |
| 36 | +/** |
| 37 | + * The Module Pattern |
| 38 | + * |
| 39 | + * The module pattern provides both private and public encapsulation for classes in conventional software engineering. |
| 40 | + * |
| 41 | + * Privacy: the module encapsulates "privacy", state and organization using closures. |
| 42 | + */ |
| 43 | + |
| 44 | +let testModule = (function () { |
| 45 | + let counter = 0; |
| 46 | + return { |
| 47 | + incrementCounter: function () { |
| 48 | + return counter++; |
| 49 | + }, |
| 50 | + resetCounter: function () { |
| 51 | + console.log("counter value prior to reset: " + counter); |
| 52 | + counter = 0; |
| 53 | + }, |
| 54 | + }; |
| 55 | +})(); |
| 56 | + |
| 57 | +testModule.incrementCounter(); |
| 58 | +testModule.resetCounter(); |
| 59 | + |
| 60 | +// Simple template covers namespace, public and private variables |
| 61 | + |
| 62 | +let myNamespace = (function () { |
| 63 | + let myPrivateVar = 0; |
| 64 | + let myPrivateMethod = function (someText) { |
| 65 | + console.log(someText); |
| 66 | + }; |
| 67 | + |
| 68 | + return { |
| 69 | + myPublicVar: "foo", |
| 70 | + |
| 71 | + myPublicFunction: function (bar) { |
| 72 | + myPrivateVar++; |
| 73 | + myPrivateMethod(bar); |
| 74 | + }, |
| 75 | + }; |
| 76 | +})(); |
| 77 | + |
| 78 | +// Another example with basket module |
| 79 | + |
| 80 | +let basketModule = (function () { |
| 81 | + let basket = []; // private |
| 82 | + function doSomethingPrivate() { |
| 83 | + //... |
| 84 | + } |
| 85 | + function doSomethingElsePrivate() { |
| 86 | + //... |
| 87 | + } |
| 88 | + return { |
| 89 | + addItem: function (values) { |
| 90 | + if (!values) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + basket.push(values); |
| 94 | + return true; |
| 95 | + }, |
| 96 | + removeItem: function (item) { |
| 97 | + if (basket.indexOf(basket.filter((e) => e.item === item.item)[0]) < 0) |
| 98 | + return false; |
| 99 | + basket = basket.filter((value) => value.item !== item.item); |
| 100 | + return true; |
| 101 | + }, |
| 102 | + getItemCount: function () { |
| 103 | + return basket.length; |
| 104 | + }, |
| 105 | + doSomethingPublic: doSomethingPrivate(), |
| 106 | + getTotal: function () { |
| 107 | + let q = this.getItemCount(); |
| 108 | + let p = 0; |
| 109 | + while (q--) { |
| 110 | + p += basket[q].price; |
| 111 | + } |
| 112 | + return p; |
| 113 | + }, |
| 114 | + }; |
| 115 | +})(); |
| 116 | + |
| 117 | +basketModule.addItem({ |
| 118 | + item: "bread", |
| 119 | + price: 0.5, |
| 120 | +}); |
| 121 | + |
| 122 | +basketModule.addItem({ |
| 123 | + item: "butter", |
| 124 | + price: 0.3, |
| 125 | +}); |
| 126 | + |
| 127 | +console.log(basketModule.getItemCount()); |
| 128 | +console.log(basketModule.getTotal()); |
| 129 | + |
| 130 | +basketModule.removeItem({ |
| 131 | + item: "bread", |
| 132 | + price: 0.5, |
| 133 | +}); |
| 134 | + |
| 135 | +console.log(basketModule.getItemCount()); |
| 136 | +console.log(basketModule.getTotal()); |
| 137 | + |
| 138 | +/** |
| 139 | + * The Revealing Module Pattern |
| 140 | + * |
| 141 | + * define all your functions and variables in the private scope and return an anonymous object with pointers |
| 142 | + */ |
| 143 | + |
| 144 | +let myRevealingModule = (function () { |
| 145 | + let name = "A Man Learns Code"; |
| 146 | + let age = 25; |
| 147 | + |
| 148 | + function updatePerson() { |
| 149 | + name = "A Man Learns Code Updated!"; |
| 150 | + } |
| 151 | + |
| 152 | + function setPerson() { |
| 153 | + name = "A Man Learns Code set"; |
| 154 | + } |
| 155 | + |
| 156 | + function getPerson() { |
| 157 | + return name; |
| 158 | + } |
| 159 | + |
| 160 | + return { |
| 161 | + // anonymous object |
| 162 | + set: setPerson, |
| 163 | + get: getPerson, |
| 164 | + }; |
| 165 | +})(); |
| 166 | + |
| 167 | +console.log(myRevealingModule.get()); |
0 commit comments