Building Multiple Levels of Prototype Objects

This section provides a tutorial example on how to build multiple levels of prototype objects. It also shows how to override an inherited property with a local property.

As a summary of object property inheritance through multiple levels of prototype objects, I wrote this tutorial example to use "myBook" as a prototype to define an "EBook" constructor function:

<!-- Inheritance_Multiple_Level_Prototypes.html
   Copyright (c) 2013 by Dr. Herong Yang,
<title>Building Multiple Level Prototypes</title>
<script type="text/javascript">
// Modifying the base prototype object

   Object.prototype.copyright = "";
<script type="text/javascript">
// Defining the "Book" constructor

function Book(title, author) {
   this.title = title; = author;
   this.getDesc = getDescription;

function getDescription() {
   return "\""+this.title+"\" by ";

   var myPrototype = new Object();
   Book.prototype = myPrototype;
   Book.prototype.price = 9.99;
   Book.prototype.constructor = Book;
   Book.prototype.toString = getString;

function getString() {
<script type="text/javascript">
// Defining the second level constructor "EBook"

function EBook(url) {
   this.url = url;
   var myBook = new Book("Programming Tutorials", "Herong Yang");
   EBook.prototype = myBook;
   Book.prototype.constructor = EBook;
// Creating an object of "EBook"
   var myEBook = new EBook("");

// Adding a local property 
   myEBook.title = "JavaScript Tutorials";

   // Showing properties of this object
   showObject(myEBook, "myEBook");

function showObject(object, name) {
   document.writeln("\n\""+name+"\" properties:");
   for (var item in object) {
      if (object.hasOwnProperty(item)) 
         var inheritance = "Local";
         var inheritance = "Inherited";
      document.writeln("   "+item+": "+inheritance+": "

The output of this tutorial example confirms that object "myEBook" inherits from "EBook.prototype", "Book.prototype", and "Object.prototype". Notice "myEBook" has a local property "title" overrides the inherited property "EBook.prototype.title":

"myEBook" properties:
   url: Local:
   title: Local: JavaScript Tutorials
   author: Inherited: Herong Yang
   getDesc: Inherited: function getDescription() {
    return "\"" + this.title + "\" by " +;
   price: Inherited: 9.99
   constructor: Inherited: function EBook(url) {
    this.url = url;
   toString: Inherited: function getString() {
    return + "/" + this.title + "/" + this.price;
   copyright: Inherited:

Table of Contents

 About This Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

 Data Types, Variables and Expressions

 Flow Control Statements

 Creating, Accessing, and Manipulating Arrays

 Defining and Calling Functions

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

 Introduction to Objects

 Defining Your Own Object Types

Inheritance of Properties and Methods through the Prototype Object Chain

 Inheriting from Constructor's Prototype Object

 Changing the Constructor's Prototype Object

 Inheriting Properties from Two Level Prototype Objects

 Built-in Properties and Methods in "Object.prototype"

 Using "hasOwnProperty()" Method to Test Inherited Properties

 Setting the "constructor" Property in the "prototype" Object

 Adding Local Methods at the Object Level

 Adding Inherited Methods at the Prototype Level

Building Multiple Levels of Prototype Objects

 Prototype Object Chain Summary

 'jrunscript' - JavaScript Shell Command from JDK

 Using Functions as "Function" Objects

 Introduction to Built-in Object Types

 W3C's Document Object Model (DOM) Specifications


 PDF Printing Version