... possible. It is one of the most useful patterns in JavaScript and has its place in
almost every project, no matter how large or small. In quick and simple projects, a singleton
can be used simply ... pat-
tern one of the most popular and widely used in JavaScript.
CHAPTER 5 ■ THE SINGLETON PATTERN 74
908Xch05.qxd 11/15/07 10:36 AM Page 74
function constructor() { // All of the normal...
... tools in the object-oriented JavaScript programmer’s
toolbox. The first principle of reusable object-oriented design mentioned in the Gang of Four’s
Design Patterns says “Program to an interface, ... useful.
In a large project, with many different programmers writing code, interfaces are essential.
Often programmers are asked to use an API that hasn’t been written yet, or are asked to...
... original JavaScript cre-
ators. The prototype attribute is meant to point to the prototype object, and through prototype
CHAPTER 4 ■ INHERITANCE48
908Xch04a.qxd 11/15/07 10: 34 AM Page 48
Using ... Person. The first four lines of the function are the
CHAPTER 4 ■ INHERITANCE 44
908Xch04a.qxd 11/15/07 10: 34 AM Page 44
Classical Inheritance
JavaScript can be made to look like a c...
... bicycle;
};
/* GeneralProductsBicycleShop class. */
var GeneralProductsBicycleShop = function() {};
extend(GeneralProductsBicycleShop, BicycleShop);
GeneralProductsBicycleShop.prototype.createBicycle ... 11/15/07 10 :41 AM Page 118
The Bridge Pattern
In the world of API implementations, bridges are incredibly useful. In fact, they’re probably
one of the most underused patterns. Of all patt...
... {
for ( var prop in styles ) {
if (!styles.hasOwnProperty(prop)) continue;
setStyle(el, prop, styles[prop]);
}
}
CHAPTER 10 ■ THE FACADE PATTERN 145
908Xch10.qxd 11/15/07 10:57 AM Page 145
var topGallery ... a collection of poorly designed
APIs by wrapping them in a single well-designed API.
JavaScript Libraries As Facades
JavaScript libraries are built for humans. They’re designed to s...
... browsers implement the console
object:
/* SimpleProfiler class. */
var SimpleProfiler = function(component) {
this.component = component;
};
SimpleProfiler.prototype = {
buildList: function() {
var ... returned result.
CHAPTER 12 ■ THE DECORATOR PATTERN1 64
908Xch12.qxd 11/15/07 11:02 AM Page 1 64
decorators with dynamic interfaces (such as MethodProfiler) can be frightening at best. You
m...
... Protection Proxy
The virtual proxy is probably the most useful type of proxy to JavaScript programmers. Let’s
briefly go over the other types and explain why they aren’t as applicable to JavaScript.
A ... trafficked
CHAPTER 14 ■ THE PROXY PATTERN206
908Xch 14. qxd 11/15/07 11:05 AM Page 206
The Proxy Pattern vs. the Decorator Pattern
A proxy is similar to a decorator in many ways. Both d...
... much easier to
access an otherwise difficult resource.
CHAPTER 14 ■ THE PROXY PATTERN2 14
908Xch 14. qxd 11/15/07 11:05 AM Page 2 14
The Observer Pattern
In an event-driven environment, such as the ... instantiate.
Benefits of the Proxy Pattern
Each type of proxy has a different set of benefits. The remote proxy allows you to treat a remote
resource as a local JavaScript object. This i...