O'Reilly logo

JavaScript Patterns by Stoyan Stefanov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required


Patterns are solutions to common problems. One step further, patterns are templates for solving categories of problems.

Patterns help you split a problem into Lego-like blocks and focus on the unique parts of the problem while abstracting out a lot of “been there, done that, got the T-shirt” kind of details.

Patterns also help us communicate better by simply providing a common vocabulary.

It’s therefore important to identify and study patterns.

Target Audience

This book is not a beginner’s book; it’s targeted at professional developers and programmers who want to take their JavaScript skills to the next level.

Some of the basics (like loops, conditionals, and closures) are not discussed at all. If you find you need to brush up on some of those topics, refer to the list of suggested reading.

At the same time, some topics (such as object creation or hoisting) may look too basic to be in this book, but they are discussed from a patterns perspective and, in my opinion, are critical to harnessing the power of the language.

If you’re looking for best practices and powerful patterns to help you write better, maintainable, robust JavaScript code, this book is for you.

Conventions Used in This Book

The following typographical conventions are used in this book:


Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.


This icon signifies a tip, suggestion, or general note.


This icon signifies a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “JavaScript Patterns, by Stoyan Stefanov (O’Reilly). Copyright 2010 Yahoo!, Inc., 9780596806750.”

If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at .

Safari® Books Online


Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.

With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.

O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:


To comment or ask technical questions about this book, send email to:

For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our website at:



I am forever indebted to the incredible reviewers who shared their energy and knowledge to make a much better book for the good of the community. Their blogs and Twitter streams are a constant source of awe, sharp observations, great ideas, and patterns.


Some of the patterns in the book were identified by the author, based on experience and on studies of popular JavaScript libraries such as jQuery and YUI. But most of the patterns are identified and described by the JavaScript community; therefore, this book is a result of the collective work of many developers. To not interrupt the narrative with history and credits, a list of references and suggested additional reading is given on the book’s companion site at http://www.jspatterns.com/book/reading/.

If I’ve missed a good and original article in the list of references, please accept my apologies and contact me so I can add it to the online list at http://jspatterns.com.


This is not a beginner’s book and some basic topics such as loops and conditions are skipped. If you need to learn more about the language, following are some suggested titles:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required