Chapter 2 The DynaScript Language


Statements

DynaScript includes statements for controlling script flow, manipulating objects, and general programming. In general, these statements follow standard C and Java syntax. They include:

Separating statements

In DynamoScripts, you do not have to separate statements with semicolons.

if-else statement

The if-else statement tests a condition, then executes one set of statements if the condition is true, and (optionally) another set of statements if it's false.

Syntax

if ( condition ) {

   statements }

[ else {

   statements } ] 

Example

if ( status == true ) {
document.WriteLn( "Your order succeeded.") ;
document.WriteLn( "Now in stock: " + quantity) ;
} else {
document.WriteLn( "Your order failed.") ;
document.WriteLn( "We only have " + quantity
+ " in stock.") ;
}

for statement

The for statement iterates over values in a loop while a condition is true.

Syntax

for ( [ initial-expression ]; [ condition ]; [ increment expression ] ) {

   statements } ;

First, the initial expression is evaluated (typically, this initializes the loop counter).

Then, the condition is evaluated. If the condition is true, the statements are executed, then the increment expression is executed. If the condition is false, the loop ends.

Example

for ( i = 1 ; i < 4 ; i++ ) {
document.WriteLn("Price of item #" +
i + " = " + price[ i ] );
}

while statement

The while statement tests a condition, and loops until it is false. The condition is tested before the first iteration of the loop.

Syntax

while ( condition ) {

   statements } ;

First, the condition is evaluated. If the condition is true, the statements are executed, and control passes back to the condition. If the condition is false, the loop ends without executing the statements again.

Example

The following loop iterates the variable i from 1 to 3:

var i = 0 ;
while ( i < 3 ) {
i++ ;
document.WriteLn("Price of item #" +
i + " = " + price[ i ] );
}

do-while statement

The do-while statement is a DynaScript extension (part of the C language, not part of the ECMAScript standard). It is similar to the while statement, except that it tests the condition after the first iteration of the loop.

Syntax

do {

   statements }

while ( condition ) ;

First, the statements are executed.

Then, the condition is evaluated. If the condition is true, control passes back to the top of the loop. If the condition is false, the loop ends.

Example

The following loop iterates the variable i from 1 to 3:

var i = 0 ;
do {
i++ ;
document.WriteLn("Price of item #" +
i + " = " + price[ i ] );
} while ( i < 3 ) ;

switch statement

The switch statement is similar to the if statement with an unconditional else clause. The only difference between the DynaScript switch statement and the C or Java implementation of the switch statement is that DynaScript does not require the cases to be unique. In the event that there are multiple matching cases only the first one will be executed. Break statements must be explicitly stated within a switch statement to stop statement execution.

Syntax

switch(expression) statement

Example

The following example uses the switch statement to count the number of vowels in a given phrase:

<!--SCRIPT switch.ssc 

function countVowels( phrase ) {
var cntA=0, cntE=0, cntI=0, cntO=0, cntU=0, cntOther=0;
var index;
var character;
var length = phrase.length;
for( index = 0; index < length; index++ ) {
character = phrase.charAt( index );
switch( character ) {
case 'A':
case 'a':
cntA++;
break;
case 'E':
case 'e':
cntE++;
break;
case 'I':
case 'i':
cntI++;
break;
case 'O':
case 'o':
cntO++;
break;
case 'U':
case 'u':
cntU++;
break;
default:
if( character >= 'a' && character <= 'z'
|| character >= 'A' && character <= 'Z' ) {
cntOther++;
}
}
}
document.WriteLn("There are " + cntA + " occurrences of the letter A" );
document.WriteLn("There are " + cntE + " occurrences of the letter E" );
document.WriteLn("There are " + cntI + " occurrences of the letter I" );
document.WriteLn("There are " + cntO + " occurrences of the letter O" );
document.WriteLn("There are " + cntU + " occurrences of the letter U" );
document.WriteLn("There are " + cntOther + " consonants" );
}

/* mainline */
countVowels( "Happy Birthday!" );


-->

break statement

The break statement ends a loop and transfers control to the statement following the loop. A break can occur in a loop or within a switch statement.

Syntax

break ;

Example

The following loop iterates from 0 to 5, then outputs 25 :

var i = 0 ;
while ( i < 10 ) {
if ( i == 5 ) {
break ;
}
i++ ;
}
document.WriteLn( i * i ) ;

continue statement

The continue statement ends execution of the block of statements in a loop, and moves on to the next iteration of the loop. In a while loop, continue jumps back to the condition. In a for loop, it jumps to the increment expression.

Syntax

continue ;

Example

The following loop iterates from 0 to 10, printing each number except 5:

var i = 0 ;
while ( i < 10 ) {
I++;
if ( i == 5 ) {
continue ;
}
document.WriteLn( i ) ;
}

for-in statement

The for-in statement is a variation of the for statement that iterates over the properties and methods of an object, executing a block of statements for each member.

Syntax

for ( variable in object ) {

   statements } 

Example

You can list the properties, property values, and methods of any object using the following simple script, where objectName is the name of the particular object:

<!--SCRIPT
for ( i in objectName ) {
document.WriteLn( i + " = " + objectName[i] ) ;
}
-->

with statement

Use the with statement for working with several properties or methods of a given object. It establishes a default object for a block of statements. Within the block, any property or methods that do not specify an object are assumed to be for the default object.

Syntax

with ( object ) {

    statements } 

Example

The following script sets the name , title , and managerName of an object called currentEmployee :

with currentEmployee {
name = "Moe Smithers" ;
title = "Bartender" ;
managerName = "Barney Burns" ;
}

class statement

The class statement is a DynaScript extension that provides a clear way of explicitly declaring an object type (a class). It also provides an optional way of deriving the new class from an existing class.

Syntax

class newClassName ( [ param ] [ , param, ... ] ) [ extends parentClassName ( [ parentParam ] [ , parentParam, ... ] )] {

    statements } ;

Example

class salariedEmployee(name, title, managerName, salary)
extends employee(name, title, managerName) {
this.salary = salary;
function PrintAllInfo() {
document.WriteLn("Name: " + this.name);
document.WriteLn("Title: " + this.title);
document.WriteLn("Reports to: " +
this.managerName);
document.WriteLn("Salary: " + this.salary);
}
this.PrintAllInfo = PrintAllInfo;
}

this keyword

The this keyword refers to the current object (in a method, the calling object) and is typically used as the qualifier for that object's properties and methods.

Syntax

this.memberName

Example

class product(id, name, size, color,
quantity, price) {
this.id = id ;
this.name = name ;
this.size = size ;
this.color = color ;
this.quantity = quantity ;
this.price = price ;
}

new operator

The new operator creates an instance of a class using the given parameters.

Syntax

objectName = new className ( [ param ] [ , param, ... ] ) ;

Example

var currentProduct = new product(600,
"Sweatshirt", "Large", "Green", 39, 24.00 ) ;

var statement

The var statement declares a variable, optionally assigning it an initial value.

The scope of a variable is the current function or (for variables declared outside a function) the current script.

You do not have to declare variables using var ; you can declare them implicitly by assigning them values on the fly. However, it is good coding practice to always use var . In addition to making your code more readable, this can avoid scoping problems. For example, if you start using an apparently local variable inside a function without declaring it, you could be inadvertently referencing an existing global variable of that name, with unexpected results.

Syntax

var varName [ = value ] [ ..., varName [ = value ] ] ;

Example

var height = 42, width = height / 2 ;

function statement

The function statement declares a function, which is a set of instructions that you can call from anywhere in a script.

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

Variable number of arguments

Functions can accept more or less parameters than are formally declared for them. Extra parameters are simply appended to the arguments array for the function.

Returning a value

The function can return a value to the caller by including a return statement.

Syntax

function name ( [ param ] [ , param, ... ] ) {

statements } 

Example

function order ( orderQuantity ) {
// order returns true if the order is successful,
// false if stock is too low
if ( this.quantity >= orderQuantity ) {
this.quantity -= orderQuantity ;
return ( true ) ;
} else {
return ( false ) ;
}
}

return statement

The return statement is used inside a function to return a value to the caller of the function.

Syntax

return expression ;

Example

You could define the following order function:

function OrderItem( orderQuantity ) {
// order returns true if the order is successful,
// false if stock is too low
if ( this.quantity >= orderQuantity ) {
this.quantity -= orderQuantity ;
return ( true ) ;
} else {
return ( false ) ;
}
}

...then call the function in an expression:

if ( OrderItem(desiredQuantity) == true ) {
document.WriteLn("Ordered successfully");
} else {
document.WriteLn("Order failed - stock is low");
}

import statement

The import statement is a DynaScript extension that, when executed, imports the text of another script at the statement's position in the current script. This allows you to store common functions in a separate script, then use them in your other scripts by importing that script.

Execution context

If you do not state and context or if you specify the newContext keyword when using the import statement, the imported script code will run in its own execution context. Only if you specify the useContext keyword will the imported code share the same variable space as the document into which it was imported. For example, imported code cannot normally reference the document or site objects, since they do not exist in its (special) execution context. Using the useContext keyword makes theses objects available to the imported code. Objects such as Math , Date and Number are still available to be referenced by imported code that uses the newContext keyword.

Syntax

import documentName | (stringExpression) [newContext | useContext] ;

Example

If you stored a set of common functions in a script called common.ssc , you could use these functions in another document by including the statement:

import "common.ssc" ;

To import a script called myScript.ssc , residing in a test folder at the root of your Web site, you could use the statements:

rootDoc = site.GetRootDocument();
import (rootDoc.location + "/test/myScript.ssc");

The following example demonstrates the sharing of variables between a main file and the file that it imports:

<!--SCRIPT main.ssc
document.writeln( "Start main.ssc" );
var foo = "This is from main.ssc";
import "importee.ssc" useContext;
document.writeln( narf );
document.writeln( "End main.ssc" );
-->

<!--SCRIPT importee.ssc
document.writeln( "Start importee.ssc" );
var narf = "This is from importee.ssc";
document.writeln( foo );
document.writeln( "End importee.ssc" );
-->

Executing main.ssc produces:

Start main.ssc
Start importee.ssc
This is from main.ssc
End importee.ssc
This is from importee.ssc
End main.ssc

comment statements

Comment statements are ignored by the script interpreter.

There are two ways to indicate comment statements in scripts:

exit statement

The exit statement stops processing the current document without affecting previous output. Portions of the document following the exit statement are not processed.

The following example template checks a user's password. If it is incorrect, it notifies the user and exits from the script. Any output generated before the exit statement was encountered still displays.

<HTML>
<TITLE>Secret Information</TITLE>
<BODY>
<!--SCRIPT
if( document.value.password != "halibut" ) {
document.writeln( "<H2>Invalid password!</H2></BODY></HTML>" );
exit;
}
-->
<H1>Secret Information</H1>
<!--SQL
select color from product
-->
<!--formatting-->
<!--/formatting-->
</BODY>
</HTML>

 


Copyright © 1999 Sybase, Inc. All rights reserved.