Example of Using an Object Prototype

The following example creates a rectangle object with an area method in a prototype:

function rectangle_area()
{
   return this.width * this.height;
}

function Rectangle(width, height)
{
   this.width = width;
   this.height = height;
}

Rectangle.prototype.area = rectangle_area;

The following code can now reference the rectangle_area method as a method of any Rectangle object:

var area1 = joe.area();
var area2 = sally.area();