CODEX
Put the JavaScript Module Design Pattern to Work
Imagine you are interviewing for a front-end developer job. You’ve just breezed through a few questions about CSS combinators, and as the conversation turns to JavaScript, the interviewer, with a mischievous glint in his eyes and an ear-to-ear smile, says this:
“So let’s talk about design patterns.”
This turn in the interview may seem daunting, but not if you’re prepared.
So let’s start with a quick rundown of what a design pattern is: a reusable solution to a commonly occurring programming problem.
Using design patterns, we can draw on well-established, time-honored techniques for writing code that can make our projects scalable and sturdy.
One such design pattern is the module design pattern. A module can refer to a range of things, but in this context, it is a self-contained component of a larger program that has everything it needs to do its job. It also can have private resources, such as variables, that cannot be directly accessed, as well as public resources, such as functions, which are exposed to the rest of the program’s environment.
Let’s Build
To explore the module design pattern further, let’s write a quick JavaScript module called fruitBasket.