HTML and CSS Reference
In-Depth Information
Objects can contain other objects as needed. In this example, the Author property could
easily be factored into a new prototype, making it more extensible and encapsulating the
information related to an author. Add the following code to the Book prototype:
Book.prototype = {
ISBN: "",
Length: -1,
genre: "",
covering: "",
author: new Author(),
currentPage: 0,
title: "",
}
function Author(){
}
function Author(firstName, lastName, gender)
{
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
}
Author.prototype = {
firstName:"",
lastName:"",
gender:"",
BookCount: 0
}
var books = new Array(new Book(),
new Book("First Edition",350, new Author("Random","Author","M"))
);
Now, the topic's Author is a custom object instead of just a string. This provides for more
extensibility in the design. If you later decide that you need to add information about the
author, you can simply add the property or properties to the Author prototype.
EXAM TIP
You can add properties to a prototype dynamically rather than use the preceding method. The
following code achieves the same outcome. Using such code is just a matter of preference.
Book.prototype.ISBN = "";
Book.prototype.Length = 350;
Book.prototype.genre = "";
Book.prototype.covering = "";
Book.prototype.author = new Author();
Book.prototype.currentPage = 0;
Book.prototype.title = "";
 
 
Search WWH ::




Custom Search