prototype property (Definition)

An internal method that returns a prototype.

Availability:

ECMAScript edition - 2
Property/method value type:Depends on the object

This property returns the prototype for the containing object.

The prototype property for an object returns a reference to the object that is the parent of the receiver in the prototype inheritance chain.

Any methods, properties, or functions present in the prototype will also be available in the child unless they are overridden.

This means that you can locate the parent prototype for a group of objects and store a value into a property there and it will be immediately available as a property of all the objects that share the same prototype.

The typical time you do this is when you are creating a prototype for a new class of object of your own. In that case you would also create a constructor function to initialize new instances of the object as well.

This property will either return null or an object that is the prototype for the object responding to the property request. This is used for maintaining the inheritance through the prototype chain.

Properties of the prototype object are exposed to the child object through the get accessor method. However since they are shared, they cannot be changed with the put accessor through the child. If they are changeable at all, the specific object that explicitly owns them is the only one with rights to modify the values as long as they are not read-only. This prevents a child object from modifying a property and that change propagating across all the objects that share the same property between them.

There is an internal property called Prototype, which yields the same value. Note the capitalization. Internal properties are not generally exposed to the scripting language. The Prototype property is one of the few that are.

Prototypes are also useful as a means of extending the interface for a particular type of object. By adding new properties and methods to the prototype, you make them available to all objects of that class.

Warnings:

Example code:

   <HTML>

   <HEAD>

   </HEAD>

   <BODY>

   <SCRIPT>

   // Make a constructor for a new class

   function Car(engineSize)

   {

      this.enginesize   = engineSize;

      this.totalMileage = 0;

   }

   // Create a special toString() override

   function myToString()

   {

      return "This is a car object";

   }

   // Register our overriding toString()

   Car.prototype.toString = myToString;

   // Make a couple of other methods to add to it

   function mileage()

   {

      return this. totalMileage;

   }

   function addMiles(newMiles)

   {

      this.totalMileage += newMiles;

   }

   // Add some shared properties

   Car.prototype.wheels = "Alloy";

   Car.prototype.body   = "Cabriolet";

   // Register the mileage methods

   Car.prototype.mileage  = mileage;

   Car.prototype.addMiles = addMiles;

   // Instantiate an object from the new class

   var myCar = new Car(2000);

   // Drive it around some to add miles to its clock

   myCar.addMiles(100);

   myCar.addMiles(150);

   myCar.addMiles(200);

   // Display its properties and call its methods

   document.write("To string : " + myCar.toString() + "<BR>");

   document.write("Engine size : " + myCar.enginesize + "<BR>");

   document.write("Wheels : " + myCar.wheels     + "<BR>");

   document.write("Body : " + myCar.body       + "<BR>");

   document.write("Mileage : " + myCar.mileage()  + "<BR>");

   </SCRIPT>

   </BODY>

   </HTML>

See also:Array.prototype, Boolean.prototype, Constructor function, constructor property, Date.prototype, Error.prototype, Function.prototype, Global object, Internal Property, Native object, Number.prototype, Object.prototype, Option.prototype, Prototype Based Inheritance, Prototype chain, RegExp.prototype, Shared Property, String.prototype

Property attributes:

DontEnum, Internal

Cross-references:

ECMA 262 edition 2 - section - 8.6.2

ECMA 262 edition 3 - section - 8.6.2

Wrox Instant JavaScript - page - 31