Vanguard Software
Forecasting & Planning Solutions

contents.gifindex.gifprev1.gifnext1.gif

Prototype Inheritance

When you use the new operator to create an object, you actually create an instance of an object of a particular class. For example, assume you start with the following object constructor

Circle(radius):={
.radius=radius;
.area=Circle_area;
}

Circle_area(none):=PI*.radius^2

and then execute the following statements:

var obj1=new Circle(5);
var obj2=new Circle(6);

The two objects obj1 and obj2 each have a property named radius. However, the two properties are separate. That is, setting the value of obj1.radius has no affect on the value of obj2.radius. When each object is created, separate memory is allocated to hold the value of the separate radius properties.

The fact that all properties in an object are separate from those of other objects is a good thing when the properties contain variable data. However, what about properties that contain static data like the area method. Both obj1 and obj2 contain a method named area and this method value is the same in both instances--it is a pointer to the Circle_area function. If you had a hundred instances of the Circle object, you would have a hundred pointers to this function. Although this structure works fine, it is a wasteful use of memory.

It would be nice if some properties could be declared globally. That is, they are defined once and all objects of the same class share the property. You can do this in DScript by assigning properties to the constructor's prototype object. Here is an example:

Circle(radius):={
.radius=radius;
Circle.prototype.area=Circle_area;
}

Note that this object constructor is almost identical to the original constructor. The only difference is that instead of assigning Circle_area to .area (same as this.area), it is assigned to Circle.prototype.area. This causes the property named area to reside with the object constructor rather than with each instance of the Circle object.

When you attempt to read the property of an object, DScript first looks for the property value in the object instance. If the property is not found, DScript looks in the prototype object attached to the object constructor. If the property is still not found, then DScript looks in the prototype object attached to the generic Object constructor. Because this hierarchy is used, you can assign properties and methods to all objects of a class by assigning the property to the constructor's prototype object.

Because methods are usually static data, they are good candidates to put in the constructor's prototype. Other properties, such as variables that are global to all objects of a class, are also good candidates for the constructor's prototype.

See Also

Property Read/Write Asymmetry

  Forecasting Software  |  Strategic Planning Software  |  Decision Support Software
Decision Tree Software  |  Monte Carlo Simulation Software  |  Risk Analysis Software
Portfolio Analysis Software  |  Expert System Software  |  Online Forms

Contact Us  |  Terms of Use  |  Privacy Policy  |  Site Map
Vanguard Software
© 2010 Vanguard Software Corporation
Home Products Solutions Services Customers Partners Company
Vanguard
Applying science to business management
Vanguard
Print this page   Email this page to a friend