Skip to content

Commit dee1f5b

Browse files
authored
Update README.md
1 parent bc034a4 commit dee1f5b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,24 @@ So all we've basically done here is improve on the example from Form 1. Declarin
186186
In short, you will often see IIFEs being used to create closure becuase it's so economical.
187187

188188
## 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+
var module = (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

Comments
 (0)