|
 |
The Classes object represents the set of DataWeb
Script classes available in the web. The Classes object is a prototype object and cannot be instantiated.
Prototype
Methods
import(classpath) |
Imports
the public members of the class specified
by the classpath argument. Returns a reference to the specified class. Returns null if classpath does not exist in the web or if the class cannot be imported. This parameter expects a string path as it value, so values for this parameter must begin with a '/'. |
|
The Classes object has a single method, the import() method. The import() method makes the public members of a class available to script in another class. In this way you can organize reusable code into classes rather than duplicating it.
The import() method returns a reference to the specified class. You can assign this reference to a variable and use it as an object in your script, as shown in the following example. The function defined below, CalcAvePrice(), imports the CommonMath.ws class from the web root. It then calls a public function from that class named average(), which finds the average of a set of numbers stored in an array.
webcall function CalcAvgPrice()
{
//Import CommonMath.ws.
var c = classes.import("/CommonMath.ws");
//Create a resultset on the Products table.
var r = new resultset("Products",
"UnitPrice", "*");
//Move through resultset and add each value
//to an array.
var arr = new Array();
while(r.next())
arr.push(r.UnitPrice);
return c.average(arr);
}
A custom script class such as CommonMath.ws functions
as a prototype object in script. That is, only a single
instance of the class ever exists in memory, no matter
how many times you import the class. In the following
code fragment, both c1 and c2 refer to the same object:
var c1 = classes.import("/CommonMath.ws");
var c2 = classes.import("/CommonMath.ws");
Because these two variables refer to the same object
in memory, any public variables declared in the CommonMath.ws
class will always have the same value, wherever they
are used. For example, in the following code fragment,
the value property for both objects will be 3.1415:
c2.value = 3.1415;
response.write(c1.value);
response.write(c2.value);
Related Topics Calling Functions
|