Siebel eScript Language Reference > Siebel eScript Commands > User-Defined Objects in Siebel eScript >

Object Prototypes in Siebel eScript


An object prototype lets you specify a set of default values for an object. When an object property that has not been assigned a value is accessed, the prototype is consulted. If such a property exists in the prototype, its value is used for the object property.

Object prototypes are useful for two reasons: they make sure that every instance of an object use the same default values, and they conserve the amount of memory needed to run a script. When the two rectangles, joe and sally, were created in the previous section, they were each assigned an area method. Memory was allocated for this function twice, even though the method is exactly the same in each instance. This redundant memory can be avoided by putting the shared function or property in an object's prototype. Then every instance of the object use the same function instead of each using its own copy of it.

The following fragment shows how to create 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 rectangle_area method can now be accessed as a method of any Rectangle object, as shown in the following:

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

You can add methods and data to an object prototype at any time. The object class must be defined, but you do not have to create an instance of the object before assigning it prototype values. If you assign a method or data to an object prototype, every instance of that object is updated to include the prototype.

If you try to write to a property that was assigned through a prototype, a new variable is created for the newly assigned value. This value is used for the value of this instance of the object's property. Other instances of the object still refer to the prototype for their values. If you assume that joe is a special rectangle, whose area is equal to three times its width plus half its height, you can modify joe as follows:

function joe_area()
{
   return (this.width * 3) + (this.height/2);
}
joe.area = joe_area;

This fragment creates a value, which in this case is a function, for joe.area that supersedes the prototype value. The property sally.area is still the default value defined by the prototype. The instance joe uses the new definition for its area method.

NOTE:  Prototypes cannot be declared inside a function scope.

Siebel eScript Language Reference