Siebel eScript Language Reference > Methods Reference > Custom Methods >

About Object Prototypes


An object prototype lets you specify a set of default values for an object. If Siebel eScript accesses an object property that is not assigned a value, then it consults the prototype. If this property exists in the prototype, and if this property contains a value, then Siebel eScript uses that value for the object property.

How an Object Prototype Conserves Memory

An object prototype helps you to make sure that every instance of an object uses the same default values and that these instances conserve the amount of memory that Siebel CRM requires to run a script. The joe and sally rectangles are each assigned an area method when they are created in How a Function Is Assigned to an Object. Siebel CRM allocates memory for this function twice, even though the method is exactly the same in each instance. To avoid this redundant memory, you can place the shared function or property in an object prototype. In this situation, every instance of the object uses the same function instead of each instance using a copy of the function.

Example of Using an Object Prototype

The following example creates 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 following code can now reference the rectangle_area method as a method of any Rectangle object:

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

Adding Methods and Data to an Object Prototype

You can write code that adds methods and data to an object prototype at any time. You must define the object class but you do not have to create an instance of the object before you assign prototype values to it. If you assign a method or data to an object prototype, then Siebel CRM updates every instance of that object to include the prototype.

If you attempt to write to a property that Siebel CRM assigns through a prototype, then it creates a new variable for the newly assigned value. It uses this value for the value of this instance of the object property. Other instances of the object still refer to the prototype for their values. The following example specifies joe as a special rectangle whose area is equal to three times the width plus half the height:

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

This code creates a value for joe.area that supersedes the prototype value. In this example, this value is a function. The sally.area property is still the default value that this prototype defines. The joe instance uses the new definition for the area method.

You cannot write code that declares a prototype in a function scope.

Siebel eScript Language Reference Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Legal Notices.