Next page | Contents page |

Inheritance

JavaScript inheritance is done by assigning an instance of the parent "class" to the prototype object. Eg,


function Person (name, address)
{ 
  this.name = name;
  this.address = address;
} 

function Staff (name, address, staffNo)
{
  this.prototype = new Person (name, address);
  this.staffNo = staffNo;
}

function Customer (name, address, orderList)
{
  this.prototype = new Person (name, address);
  this.orderList = orderList;
} 

In this way objects of type Staff and Customer inherit all aspects (state and behaviour) of the supertype Person. Any instance of Staff or Customer is also an instance of Person. A Customer IS A Person.

Staff and Customer are said to be subclasses of Person.

But JavaScript really doesn't have classes - only instances (objects). Cunning!

Next page | Contents page |