JS Built-ins
Modulesintermediate

The Module Pattern (IIFE)

Before ES modules, developers used IIFEs (Immediately Invoked Function Expressions) to create private scope. The module pattern returns a public API while keeping implementation private: const counter = (() => { let count = 0; return { increment() { count++; }, get() { return count; } }; })();

πŸ’‘ Hints (5)
  • β€’ Use an IIFE (Immediately Invoked Function Expression)
  • β€’ Define count as a private variable inside the IIFE
  • β€’ Return an object with increment() and getCount() methods
  • β€’ increment() should increase count by 1
  • β€’ getCount() should return the current count