O'Reilly logo

Pro JavaScript™ Design Patterns by Dustin Diaz, Ross Harmes

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

CHAPTER 12The Decorator Pattern

In this chapter, we look at a way to add features to objects without creating new subclasses. The decorator pattern is used to transparently wrap objects within another object of the same interface. This allows you to add behavior to a method and then pass the method call on to the original object. Using decorator objects is a flexible alternative to creating subclasses. This pattern is well-suited to JavaScript (as you will see later in the chapter with dynamic interfaces) because typical JavaScript code does not rely heavily on the types of objects.

The Structure of the Decorator

A decorator allows you to add functionality to an object. This can be used in place of large numbers of subclasses. To illustrate ...

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