HTML and CSS Reference
In-Depth Information
assertEquals(observer1, calls[0]);
assertEquals(observer2, calls[1]);
Because the implementation already uses an array for the observers, this test
succeeds immediately.
11.6 Observing Arbitrary Objects
In static languages with classical inheritance, arbitrary objects are made observable
by subclassing the Observable class. The motivation for classical inheritance
in these cases comes from a desire to define the mechanics of the pattern in one
place and reuse the logic across vast amounts of unrelated objects. As discussed
in Chapter 7, Objects and Prototypal Inheritance, we have several options for code
reuse among JavaScript objects, so we need not confine ourselves to an emulation
of the classical inheritance model.
Although the Java analogy helped us develop the basic interface, we will now
break free from it by refactoring the observable interface to embrace JavaScript's
object model. Assuming we have a Newsletter constructor that creates
newsletter objects, there are a number of ways we can make newsletters observ-
able, as seen in Listing 11.31.
Listing 11.31 Various ways to share observable behavior
var Observable = tddjs.util.Observable;
// Extending the object with an observable object
tddjs.extend(newsletter, new Observable());
// Extending all newsletters with an observable object
tddjs.extend(Newsletter.prototype, new Observable());
// Using a helper function
// Calling the constructor as a function
// Using a "static" method:
Search WWH ::

Custom Search