Summary
In this chapter, we discussed some ways to organize the code of a complex JavaScript application. We started by analyzing the global scope as a repository of shared data and agreed how it is better to minimize its use and to find a way to avoid name collisions. So, we introduced the concept of namespace and implemented it as object literals and as IIFE.
Then, we started to discuss the concept of module as the basic organizational unit that many languages supports natively. The lack of this construct in JavaScript before ECMAScript 6 specification has given rise to several proposals. We explored the module pattern, the classic approach based on the closure of an IIFE, and analyzed the various approaches in augmenting and combining modules. Then, we talked about the difference between script and module loading and discussed the two main paradigms to loading modules the CommonJS module and the Asynchronous Module Definition. We saw how these two approaches define incompatible module...