| Applying science to business management |
  
Property Read/Write Asymmetry
When you put a property in a constructor's prototype object,
the property can be read just as if it were part of each object
instance created with that constructor. That is, you can access
the area method in the example above using the expression obj1.area() no matter where area
was defined (constructor prototype or object instance). However,
when you write to a property the prototype properties are not
accessible. Instead, writing to a property that has the same name
as a prototype property will create a local property in that
object instance instead of modifying the constructor's prototype
property.
Assume you start with the following object constructor:
Circle(radius):={
.radius=radius;
Circle.prototype.area=Circle_area;
}
Circle_area(none):=PI*.radius^2
The following statements illustrate how various versions of
the area method are accessed:
var obj=new Circle(5); // create
a new object
obj.area(); // call Circle_area
obj.area=My_area; // create new
method in obj
obj.area(); // call My_area
delete obj.area; // delete local
area method
obj.area(); // call Circle_area
Circle.prototype.area=My_area;// replace
prototype method
obj.area(); // call My_area
In the example above, you can read the value of the
constructor's prototype method named area using the
expression
obj.area
However, to write to this method, you must use the expression
Circle.prototype.area
In contrast, the local method named area that is stored
in the object instance can be accessed for both reading and
writing using the expression
obj.area
|