Siebel eScript Language Reference > Methods Reference > Custom Methods >

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 method uses the this operator to reference a method 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 a method to an object:

joe.area = rectangle_area;

The function now uses the values for height and width that were defined when you created the joe rectangle object.

To assign a method in a constructor function, you can use the this keyword. The following example creates an object class named Rectangle that includes the rectangle_area method as a property:

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 = Rectangle(3,4);
var sally = Rectangle(5,3);

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

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