JavaScript
/Intermediate
Module Pattern
Definition
A design pattern used to emulate the concept of classes and true encapsulation (private variables) in JavaScript using Closures and IIFEs.
Explain Like I'm New
Before modern JavaScript had the `import/export` syntax, everything was global. The Module Pattern was a clever trick to put your variables in a hidden box, and only cut a small hole in the box to let specific functions (the public API) out.
Real World Example
Creating a Shopping Cart where the `items` array is completely hidden from the rest of the application. The only way to interact with it is via the exposed `addItem()` and `getTotal()` methods.
Common Use Cases
- •Encapsulation
- •Data Privacy
- •Legacy codebase architecture
Interactive Example
Loading...
Console output will appear here...
Interview Questions
basic
- What is Encapsulation?
intermediate
- How does the Module Pattern achieve privacy?
advanced
- Is the Module Pattern still necessary with ES6 Modules?