03.09.2015 Views

Design Patterns

Download - Assembla

Download - Assembla

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

46<br />

CHAPTER 4 ■ INHERITANCE<br />

Instead of using a constructor function named Person to define the class structure, Person<br />

is now an object literal. It is the prototype object for any other Person-like objects that you want<br />

to create. Define all attributes and methods you want these objects to have, and give them<br />

default values. For the methods, those default values will probably not be changed; for attributes,<br />

they almost certainly will be:<br />

var reader = clone(Person);<br />

alert(reader.getName()); // This will output 'default name'.<br />

reader.name = 'John Smith';<br />

alert(reader.getName()); // This will now output 'John Smith'.<br />

To create a new Person-like object, use the clone function (we go into more detail about<br />

this function later in the section “The clone Function”). This provides an empty object with<br />

the prototype attribute set to the prototype object. This means that if any method or attribute<br />

lookup on this object fails, that lookup will instead look to the prototype object.<br />

To create Author, you don’t make a subclass of Person. Instead you make a clone:<br />

/* Author Prototype Object. */<br />

var Author = clone(Person);<br />

Author.books = []; // Default value.<br />

Author.getBooks = function() {<br />

return this.books;<br />

}<br />

The methods and attributes of this clone can then be overridden. You can change the<br />

default values given by Person, or you can add new attributes and methods. That creates a new<br />

prototype object, which you can then clone to create new Author-like objects:<br />

var author = [];<br />

author[0] = clone(Author);<br />

author[0].name = 'Dustin Diaz';<br />

author[0].books = ['JavaScript <strong>Design</strong> <strong>Patterns</strong>'];<br />

author[1] = clone(Author);<br />

author[1].name = 'Ross Harmes';<br />

author[1].books = ['JavaScript <strong>Design</strong> <strong>Patterns</strong>'];<br />

author[1].getName();<br />

author[1].getBooks();<br />

Asymmetrical Reading and Writing of Inherited Members<br />

We mentioned before that in order to use prototypal inheritance effectively, you must forget<br />

everything you know about classical inheritance. Here is one example of that. In classical<br />

inheritance, each instance of Author has its own copy of the books array. You could add to it by<br />

writing author[1].books.push('New Book Title'). That is not initially possible with the object<br />

you created using prototypal inheritance because of the way prototype chaining works. A clone<br />

is not a fully independent copy of its prototype object; it is a new empty object with its prototype

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!