IIFE (Immediately Invoked Function Expression)
What is the relationship between IIFEs and the module pattern in JavaScript?
The module pattern is a specific application of IIFEs to create encapsulated modules with private and public members. The classic JavaScript module pattern uses an IIFE that returns an object containing public methods and properties, while keeping private variables and functions hidden inside the closure. This pattern was extremely important in pre-ES6 JavaScript when there was no native module system. It allowed developers to create reusable, encapsulated code blocks with clear interfaces and hidden implementation details. The pattern looks something like this: ```javascript var myModule = (function() { // Private members var privateVar = 'private'; function privateMethod() { return privateVar; } // Public interface return { publicVar: 'public', publicMethod: function() { return privateMethod(); } }; })(); ``` While ES6 modules have largely replaced this pattern for new code, understanding the module pattern is still valuable for working with legacy code and understanding JavaScript's evolution.