Carl Danley

The Module Pattern

This pattern is used to mimic classes in conventional software engineering and focuses on public and private access to methods & variables. The module pattern strives to improve the reduction of globally scoped variables, thus decreasing the chances of collision with other code throughout an application.

The module pattern is, by far, the most commonly used design pattern and widely accepted in a number of large projects such as jQuery, Dojo, ExtJS and YUI.

"I'm currently in the process of writing a large-scale JavaScript application based on this pattern, and I have to say, it's a delight."
- Brian Cray

This article is part of a series called JavaScript Design Patterns.

Advantages

Disadvantages

Example

Another example of the module pattern that exposes the module a little differently and makes use of a shared private cache. This method encourages more of an object creation approach where we can optimize performance by being efficient with shared storage.

Additional Resources

http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html
http://macwright.org/2012/06/04/the-module-pattern.html
http://briancray.com/posts/javascript-module-pattern
http://css-tricks.com/how-do-you-structure-javascript-the-module-pattern-edition/
http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript

Other Common JS Patterns

The Module Pattern
The Revealing Module Pattern
The Singleton Pattern
The Observer Pattern
The Mediator Pattern
The Prototype Pattern
The Facade Pattern
The Factory Pattern