Chapter 2 The DynaScript Language


Functions

You can define and use functions in scripts. This allows you to encapsulate a set of instructions once in a document as a function, then use it several times throughout the document (by calling the function). For example:

<!--SCRIPT
function addVar( x, y, z )
{
return x + y + z;
}

document.writeln( addVar( 1, 2, 3 ) );
-->

Note  

Define functions before using them
You must define a function before it can be used, since the application server processes an HTML document from top to bottom.

A function can accept string, number, or object parameters.

Note  

Parentheses must be used
You must use parentheses when calling a function, even if it doesn't take away arguments.

A function can perform a set of statements without returning a value. For example:

PrintInvoices();

A function can also return a value to the caller by including a return statement. In this case, the function is typically called as part of an expression:

nextCustomerName = GetCustomerName(customerNum);

You can define functions inside a particular document. For more flexibility, you can define them in a separate script, which can then be referenced by any documents that use the function (using the import statement).

For more information on the function , return , and import statements, see "Statements".

Functions and the arguments object

The arguments object may be used with a function to return an array that contains the arguments that were passed to the currently executing function. For Example:

<!--SCRIPT
function addVar( x, y, z )
{
document.writeln( addVar.arguments[0] );
document.writeln( arguments );
return x + y + z;
}

document.writeln( addVar( 1, 2, 3 ) );
-->

would return

1
{ 1, 2, 3 }
6

The arguments object has a length property which contains the number of variables passed to the function.

The arguments object also has a callee property which may be used for recursion. The following example creates a function dynamically and then recursively calls the same function from within the function body:

<!--SCRIPT 
f = new Function ( "n", " if(n == 1) return 1; return n * arguments.callee(n-1);" );
document.WriteLn( f(4) );
-->

Built-in functions

Dynamo supports the following ECMAScript built-in functions:

Dynamo-specific built-in functions

Dynamo provides the following built-in functions:

This example uses toXMLString to display automobile service information:

<!--SCRIPT
document.WriteLn("Car for Service")
carObj = null;
carObj.manufact = "Mazda";
carObj.model = "MX6";
carObj.year = "1998";
carObj.color = "blue";
carObj.owner.name = "Rick Smith";
carObj.owner.address = "21 King St. Waterloo";
carObj.work = "new brakes";
information = toXMLString( carObj );
document.WriteLn( information );
-->

The output from the above example would look something like this:

Car for service
<manufac>Mazda</manufac>
<model>MX6</model>
<year>1998</year>
<color>blue</color>
<owner><name>Rick Smith<name>
<address>21 King St. Waterloo<address></owner>
<work>new brakes</new brakes>

The following example of the toXMLString built-in function sets use_CDATA to true and specifies which tag to use if a member has not been named:

<!--SCRIPT
document.WriteLn("Parts for order");
part1.shop = "Jim & Son's Autobody";
part1.partname = "strut";
part1.sku = "123";

part2.shop = "Jim & Son's Autobody";
part2.partname = "clutch";
part2.sku = "456";

parts[0] = part1;
parts[1] = part2;
x = toXMLString(parts, true, "part");
document.WriteLn( x);
-->

The output from the above example would look something like:

Parts for Order
<part>
<shop><![CDATA[Jim & Son's Autobody]]></shop>
<partname>strut</partname>
<sku>123</sku>
</part>
<part><shop><![CDATA[Jim & Son's Autobody]]></shop>
<partname>clutch</partname>
<sku>456</sku>
</part>

 


Copyright © 1999 Sybase, Inc. All rights reserved.