Chapter 6. Inter-module interaction
This chapter covers
- A review of modules
- Interaction between modules
- Using module dependencies
- Using the publish/subscribe pattern
In chapter 3, you learned a great deal about modular programming. One of the biggest takeaways from that chapter is the idea that you can internalize the complexity of your code and provide a public API to its features by applying an architectural design pattern commonly referred to as the module pattern. This is a way to achieve encapsulation in JavaScript.
As you discovered, coding with modules helps organize your application’s logic into small, single-purpose units, which are easier to maintain and update. This inevitably leads to greater reusability for your code. Using modules also helps with data integrity, code organization, and the avoidance of name collisions. After all, you’re creating code in a single, nonrefreshing page. Without this kind of design for your application’s code base, relying purely on global variables and functions would quickly become unmanageable (see figure 6.1).