You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -186,3 +186,24 @@ So all we've basically done here is improve on the example from Form 1. Declarin
186
186
In short, you will often see IIFEs being used to create closure becuase it's so economical.
187
187
188
188
## Form 3 The modular pattern
189
+
The modular pattern takes the example in Form 2 one step further. Take a look at the example below:
190
+
191
+
```javascript
192
+
varmodule= (function(){
193
+
// Private variable, sometimes marked with an underscore
194
+
var _foo ="foo";
195
+
196
+
return {
197
+
// These will be a public functions to get and set _foo
198
+
getFoo:function(){ return _foo },
199
+
setFoo:function(newVal) { _foo = newVal }
200
+
}
201
+
})();
202
+
203
+
module.getFoo(); // "foo"
204
+
module.setFoo("bar");
205
+
module.getFoo(); // "bar"
206
+
console.log(module._foo); // undefined
207
+
```
208
+
209
+
Our IIFE is now returning an object with a set of functions, not just one function. The modular pattern returns an object. Anything that gets returned in the object with be public. Anything from the IIFE that is not returned in the object will be private.
0 commit comments