How a Function Is Assigned to an Object
An object can contain a function and variables. A function assigned to an object is a method of that object.
A function defining a method uses the this operator to reference an object variable. The following example is a method that computes the area of a rectangle:
function rectangle_area()
{
return this.width * this.height;
}
Siebel CRM passes no arguments to this function, so it is meaningless unless an object calls it. This object provides values for this.width and this.height.
The following code assigns the method to an object:
function rectangle_area()
{
return (this.width * this.height);
}
function Rectangle(width,height)
{
this.width = width;
this.height = height;
this.area = rectangle_area;
}
The method is available to any instance of the class. The following example sets the value of area1 to 12 and the value of area2 to 15:
var joe = new Rectangle(3,4);
var sally = new Rectangle(5,3);
var area1 = joe.area;
var area2 = sally.area;