HTML and CSS Reference
In-Depth Information
// Telling the object to "fix itself" (requires code on
// the prototype of either Newsletter or Object)
// Classical inheritance-like
In the interest of breaking free of the classical emulation that constructors
provide, consider the examples in Listing 11.32, which assume that tddjs.
util.observable is an object rather than a constructor.
Listing 11.32 Sharing behavior with an observable object
// Creating a single observable object
var observable = Object.create(tddjs.util.observable);
// Extending a single object
tddjs.extend(newspaper, tddjs.util.observable);
// A constructor that creates observable objects
function Newspaper() {
/* ... */
Newspaper.prototype = Object.create(tddjs.util.observable);
// Extending an existing prototype
tddjs.extend(Newspaper.prototype, tddjs.util.observable);
Simply implementing the observable as a single object offers a great deal of
flexibility. To get there we need to refactor the existing solution to get rid of the
11.6.1 Making the Constructor Obsolete
To get rid of the constructor we should first refactor Observable such that
the constructor doesn't do any work. Luckily, the constructor only initializes the
observers array, which shouldn't be too hard to remove. All the methods on
Observable.prototype access the array, so we need to make sure they can all
handle the case in which it hasn't been initialized. To test for this we simply need to
write one test per method that calls the method in question before doing anything
Search WWH ::

Custom Search