My co-developers at my previous job were not terribly JavaScript-inclined and I found myself describing a lot of the information below to them any time they looked at JavaScript code with me. I figured I would write it all down as reference.
Not that this is original. YUI talked about it back in 2007. Ben Cherry goes into much greater detail. Jonathan Snook is not a big fan. On and on. I’m adding my two cents because I think it is important to first mention the aspects of JavaScript that make the pattern possible.
The module pattern is extremely useful for namespacing your site’s functionality to help prevent your code from overwriting others’ code (and vice versa). Instead of putting your functionality on the global object where it can easily clobber or be clobbered by other globals, you tuck it away in an extensible module. And if necessary, you can have modules within modules within modules.
But first, a few key JavaScript concepts that enable and naturally lead to the module pattern.