Skip to content

Commit

Permalink
module pattern utilizes an IIFE (#208)
Browse files Browse the repository at this point in the history
The module pattern is not just similar to an IIFE but utilizes one.
  • Loading branch information
Daniel-Hug authored and addyosmani committed Jul 29, 2017
1 parent 4fd3208 commit eb4ac03
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion book/index.html
Expand Up @@ -939,7 +939,7 @@ <h4>Privacy</h4>
The Module pattern encapsulates "privacy", state and organization using closures. It provides a way of wrapping a mix of public and private methods and variables, protecting pieces from leaking into the global scope and accidentally colliding with another developer's interface. With this pattern, only a public API is returned, keeping everything else within the closure private. </p>

<p>
This gives us a clean solution for shielding logic doing the heavy lifting whilst only exposing an interface we wish other parts of our application to use. The pattern is quite similar to an immediately-invoked functional expression (<a href="http://benalman.com/news/2010/11/immediately-invoked-function-expression/">IIFE</a> - see the section on namespacing patterns for more on this) except that an object is returned rather than a function. </p>
This gives us a clean solution for shielding logic doing the heavy lifting whilst only exposing an interface we wish other parts of our application to use. The pattern utilizes an immediately-invoked function expression (<a href="http://benalman.com/news/2010/11/immediately-invoked-function-expression/">IIFE</a> - see the section on namespacing patterns for more on this) where an object is returned. </p>

<p>
It should be noted that there isn't really an explicitly true sense of "privacy" inside JavaScript because unlike some traditional languages, it doesn't have access modifiers. Variables can't technically be declared as being public nor private and so we use function scope to simulate this concept. Within the Module pattern, variables or methods declared are only available inside the module itself thanks to closure. Variables or methods defined within the returning object however are available to everyone.
Expand Down

0 comments on commit eb4ac03

Please sign in to comment.