Search and Find
Service
Contents at a Glance
5
Contents
6
About the Authors
13
About the Technical Reviewer
14
Acknowledgments
15
Introduction
16
Who This Book Is For
16
How This Book Is Structured
17
Prerequisites
20
Downloading the Code
21
Contacting the Authors
21
Object-Oriented JavaScript
22
Expressive JavaScript
23
The Flexibility of JavaScript
23
A Loosely Typed Language
26
Functions As First- Class Objects
26
The Mutability of Objects
28
Inheritance
29
Design Patterns in JavaScript
29
Summary
30
Interfaces
31
What Is an Interface?
31
How Other Object- Oriented Languages Handle Interfaces
32
Emulating an Interface in JavaScript
34
The Interface Implementation for This Book
38
The Interface Class
39
Patterns That Rely on the Interface
43
Summary
43
Encapsulation and Information Hiding
44
The Information Hiding Principle
44
Basic Patterns
45
More Advanced Patterns
54
Benefits of Using Encapsulation
58
Drawbacks to Using Encapsulation
58
Summary
59
Inheritance
60
Why Do You Need Inheritance?
60
Classical Inheritance
61
Prototypal Inheritance
64
Comparing Classical and Prototypal Inheritance
68
Inheritance and Encapsulation
68
Mixin Classes
69
Example: Edit- in- Place
71
When Should Inheritance Be Used?
81
Summary
82
The Singleton Pattern
83
The Basic Structure of the Singleton
83
Namespacing
84
A Singleton As a Wrapper for Page- Specific Code
86
A Singleton with Private Members
88
Lazy Instantiation
93
Branching
96
Example: Creating XHR Objects with Branching
97
When Should the Singleton Pattern Be Used?
99
Benefits of the Singleton Pattern
99
Drawbacks of the Singleton Pattern
100
Summary
100
Chaining
101
The Structure of a Chain
102
Building a Chainable JavaScript Library
104
Using Callbacks to Retrieve Data from Chained Methods
107
Summary
108
Design Patterns
109
The Factory Pattern
110
The Simple Factory
110
The Factory Pattern
113
When Should the Factory Pattern Be Used?
116
Example: XHR Factory
116
Example: RSS Reader
121
Benefits of the Factory Pattern
124
Drawbacks of the Factory Pattern
125
Summary
125
The Bridge Pattern
126
Example: Event Listeners
126
Other Examples of Bridges
127
Bridging Multiple Classes Together
128
Example: Building an XHR Connection Queue
128
When Should the Bridge Pattern Be Used?
139
Benefits of the Bridge Pattern
140
Drawbacks of the Bridge Pattern
140
Summary
140
The Composite Pattern
141
The Structure of the Composite
142
Using the Composite Pattern
142
Example: Form Validation
143
Example: Image Gallery
152
Benefits of the Composite Pattern
155
Drawbacks of the Composite Pattern
155
Summary
156
The Facade Pattern
157
Some Facade Functions You Probably Already Know About
157
JavaScript Libraries As Facades
158
Facades As Convenient Methods
159
Example: Setting Styles on HTML Elements
160
Example: Creating an Event Utility
162
General Steps for Implementing the Facade Pattern
163
When Should the Facade Pattern Be Used?
164
Benefits of the Facade Pattern
164
Drawbacks of the Facade Pattern
164
Summary
164
The Adapter Pattern
165
Characteristics of an Adapter
165
Adapting Existing Implementations
166
Example: Adapting One Library to Another
166
Example: Adapting an Email API
168
When Should the Adapter Pattern Be Used?
174
Benefits of the Adapter Pattern
174
Drawbacks of the Adapter Pattern
174
Summary
174
The Decorator Pattern
175
The Structure of the Decorator
175
In What Ways Can a Decorator Modify Its Component?
180
The Role of the Factory
185
Function Decorators
188
When Should the Decorator Pattern Be Used?
189
Example: Method Profiler
189
Benefits of the Decorator Pattern
192
Drawbacks of the Decorator Pattern
192
Summary
193
The Flyweight Pattern
194
The Structure of the Flyweight
194
Example: Car Registrations
194
Managing Extrinsic State
198
Example: Web Calendar
198
Example: Tooltip Objects
201
Storing Instances for Later Reuse
205
When Should the Flyweight Pattern Be Used?
207
General Steps for Implementing the Flyweight Pattern
208
Benefits of the Flyweight Pattern
208
Drawbacks of the Flyweight Pattern
209
Summary
209
The Proxy Pattern
211
The Structure of the Proxy
211
When Should the Proxy Be Used?
215
Example: Page Statistics
215
General Pattern for Wrapping a Web Service
219
Example: Directory Lookup
220
General Pattern for Creating a Virtual Proxy
224
Benefits of the Proxy Pattern
227
Drawbacks of the Proxy Pattern
227
Summary
228
The Observer Pattern
229
Example: Newspaper Delivery
229
Building an Observer API
232
Observers in Real Life
234
Example: Animation
235
Event Listeners Are Also Observers
236
When Should the Observer Pattern Be Used?
237
Benefits of the Observer Pattern
237
Drawbacks of the Observer Pattern
237
Summary
237
The Command Pattern
238
The Structure of the Command
238
Types of Command Objects
241
Example: Menu Items
243
Example: Undo and Logging
248
When to Use the Command Pattern
255
Benefits of the Command Pattern
256
Drawbacks of the Command Pattern
256
Summary
257
The Chain of Responsibility Pattern
258
The Structure of the Chain of Responsibility
258
Passing on Requests
264
Implementing a Chain of Responsibility in an Existing Hierarchy
267
Event Delegation
268
When Should the Chain of Responsibility Pattern Be Used?
268
Example: Image Gallery Revisited
269
Benefits of the Chain of Responsibility Pattern
274
Drawbacks of the Chain of Responsibility Pattern
275
Summary
275
Index
276
All prices incl. VAT