Chapter 2 The DynaScript Language
DynaScript includes statements for controlling script flow, manipulating objects, and general programming. In general, these statements follow standard C and Java syntax. They include:
if-else
for
while
, do-while
switch
break
and continue
for-in
with
class
, this
,
and new
var
function
, return
import
comments
exit
In DynamoScripts, you do not have to separate statements with semicolons.
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.
if ( condition ) {
statements }
[ else {
statements } ]
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.") ;
}
The for
statement
iterates over values in a loop while a condition is true.
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.
i
from
1 to 3:
for ( i = 1 ; i < 4 ; i++ ) {
document.WriteLn("Price of item #" +
i + " = " + price[ i ] );
}
The while
statement
tests a condition, and loops until it is false. The condition is
tested before
the first iteration of the loop.
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.
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 ] );
}
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.
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.
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 ) ;
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.
switch(expression) statement
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!" );
-->
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.
break ;
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 ) ;
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.
continue ;
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 ) ;
}
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.
for ( variable in object ) {
statements }
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] ) ;
}
-->
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.
with ( object ) {
statements }
The following script sets the name
, title
,
and managerName
of an object called currentEmployee
:
with currentEmployee {
name = "Moe Smithers" ;
title = "Bartender" ;
managerName = "Barney Burns" ;
}
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.
class newClassName ( [ param ] [ , param, ... ] ) [ extends parentClassName ( [ parentParam ] [ , parentParam, ... ] )] {
statements } ;
this
keyword
to reference the new class. For example:
this.name = incomingName ;
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;
}
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.
this.memberName
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 ;
}
The new
operator
creates an instance of a class using the given parameters.
objectName = new className ( [ param ] [ , param, ... ] ) ;
var currentProduct = new product(600,
"Sweatshirt", "Large", "Green", 39, 24.00 ) ;
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.
var varName [ = value ] [ ..., varName [ = value ] ] ;
var height = 42, width = height / 2 ;
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.
arguments
array
property of the function. This array indexes all actual parameters
starting at 0. For example, you could access the third parameter
of a function called MyFunction
as MyFunction.arguments[2]
.
arguments.length
.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.
The function can return a value to the caller
by including a return
statement.
function name ( [ param ] [ , param, ... ] ) {
statements }
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 ) ;
}
}
The return
statement
is used inside a function to return a value to the caller of the
function.
return expression ;
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");
}
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.
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.
import documentName | (stringExpression) [newContext | useContext] ;
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 are ignored by the script interpreter.
There are two ways to indicate comment statements in scripts:
/*
and */
is
a comment. For example:
/* This is a comment
that extends
over three lines */
//
constitute
a comment. Characters on the next line are interpreted by the script interpreter.
For example:
x = 365 ; // Explanatory remarks can be placed here.
y = x / 12 ;
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. |
![]() |