Module Pattern in JavaScript and CoffeeScript
One of the shortcomings of JavaScript objects is the lack of support for private members. The ability to create private members makes externalAPIs more meaningful and helps keep code more manageable and DRY.
TheModule Pattern, pioneered by Douglas Crockford while building the YUI library, is a mechanism that allows for creating public and private members in JavaScript.
This post looks at the pattern in both JavaScript and CoffeScript.
The object literal
The foundational element of the Module Pattern is theobject literal, which provides the ability to create instances of objects without instantiating them.
One of the big wins provided by object literals is the ability to add namespaces to your JavaScript code. When you declare a variable outside of an object literal or function the variable is assigned to thewindow
object. In this example, thewindow
object, which represents the open browser window, can be thought of as an implied global namespace. However, writing variables and functions in the defaultwindow
namespace can be problematic.
Namespaces
#"http://briancrescimanno.com/2009/09/24/how-self-executing-anonymous-functions-work/">self-executingfunction to make it difficult to accidentally declare variables onwindow
.
CoffeeScript:
// on the window objectwindow.aFunction = -> console.log(‘My function’)window.aFunction = -> console.log('I just overwrote my original function!')window.aFunction() // 'I just overwrote my original function!'// taking advantage of object literals to provide namespaceswindow.Namespace = {};window.AnotherNamespace = {};Namespace.aFunction = -> console.log('My function')AnotherNamespace.aFunction = -> console.log('I did not overwrite my original function!')Namespace.aFunction() // prints 'My function'AnotherNamespace.aFunction() // 'I did not overwrite my original function!'
Closures
Now that we’ve covered object literals and namespaces let’s take a look at thereal power the Module Pattern provides. Using JavaScript’s closures, themethods and variables can be declared not publicly accessible.
#"#self-executing-functions"> Self-executing functions
We can also use self-executing functions to create instances of prototypesthat share private members.
#"Application Programming Interface">API can make your JavaScript easierto read. Using the Module Pattern requires additional effort up front, but paysfor itself in reduced maintenance down the line.
If you enjoyed this post, you might also like:
About thoughtbot
We've been helping engineering teams deliver exceptional products for over 20 years. Our designers, developers, and product managers work closely with teams to solve your toughest software challenges through collaborative design and development.Learn more about us.

Upgrade your codebase with a Code Audit
Learn how we can help you understand the current state of your code quality, speed up delivery times, improve developer happiness, and level up your user experience
Learn more about a Code Audit