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.
Do not create your own object properties with this name. You can assign new values to the prototype property in some implementations if you want to modify the inheritance chain but you should not use this property name for any other purpose.
You cannot assign values to the prototype of the native objects in MSIE version 3.
Be careful if you are adding to the prototype for some of the more obscure classes as this may reveal some shortcomings in the prototype-constructor mapping, which is implemented incorrectly for some objects.
<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>
ECMA 262 edition 2 - section - 8.6.2
ECMA 262 edition 3 - section - 8.6.2
Wrox Instant JavaScript - page - 31
Prev | Home | Next |
prototype.toString() | Up | Proxies |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |