The revealing module pattern
Before diving straight into ES modules, it’s worth taking a brief detour to explore a foundational JavaScript pattern, the revealing module pattern, a pattern that facilitates information hiding and will be useful in building a primitive module system. This background will not only deepen our appreciation of fully-fledged module systems like ES modules and CommonJS but also provide a great opportunity to see how these module systems can be implemented behind the scenes. As such, this pattern will become a foundational piece of knowledge that will help us to deeply understand ES modules.
As we said, modules are the bricks for structuring non-trivial applications and the main mechanism to enforce information hiding by keeping all the functions and variables that are not explicitly marked to be exported private.
One major issue with JavaScript in the browser is the lack of namespacing. Since every script runs in the global scope, both internal...