Quick Nav
See Also
Ejscript Statements
Ejscript statements are similar to their C/C++ counterparts but with some object oriented
additions. Statements represent the body of a Ejscript program by combining logic with expressions.
Ejscript implements a subset of the statements defined by the ECMAScript specification. The following statements are not supported: switch, while, do, break, continue and with. To minimize memory footprint, statements such as while, have not been implemented as the for statement provides equivalent functionality.
For example:
You can put any expression or statement in the initialization or increment sections. In this example, we simply define a variable i and initialize it to zero in the initialization section and increment it each time after the statement body is executed. The conditional expression must evaluate to a boolean value which if true allows the statement body to be executed. The increment expression is evaluated after the statement body is executed and before the conditional is retested.
This statement will execute the statement body for each property name in the object. Each time, the variable will be set to the name of the next property in the object. The order of the walk through all the properties is determined by the order of property creation. NOTE: it will not be set to the property value, but will be set to the property name. For example:
This statement will execute the statement body for each property in the object. Each time, the variable will be set to the next property in the object. The order of the walk through all the properties is according to the order of property creation.
This will print the value of each property defined in the customer object.
The function name must be an identifier and not a reserved word and is followed by an optional list of arguments. Between braces, a set of statements define the function body. For example:
Function declarations can also be nested, i.e. a function may be defined in the statement body within an outer function. In this manner, the inner function will only be visible within the scope of the outer function.
When the function is invoked a new local variable store is created so that any variables declared and used in the function will be private to the function. Functions invoke other functions and each function will have its own local variables. If a variable is assigned to without using the var statement, the variable will be created in the global variable store.
The expression is evaluated and if true, the first statement is executed. If an else phrase is added and the expression evaluates to false, then the else statement will be executed.
Statements may be grouped using braces. For example:
The conditional expression may be a compound conditional expression. For example:
EJS uses lazy evaluation where if the first expression (i < j) is true, then the following expressions will not be evaluated. In the example above, getToday() will not be called as i is less than j.
A return expression may be a simple value or it may be an object reference. For example:
For example:
If an initializer value is not defined, the identifier will be set to the undefined value.
If the var statement is used within a function, the variable is defined in the local variable store. If it is used outside a function, the variable is defined on the global store. If a variable is assigned to without a var statement, then the variable is created on the global store. For example:
This code snippit will print "x is 7".
Ejscript implements a subset of the statements defined by the ECMAScript specification. The following statements are not supported: switch, while, do, break, continue and with. To minimize memory footprint, statements such as while, have not been implemented as the for statement provides equivalent functionality.
Statement Overview
The following table describes the major statements supported by Ejscript.
| Statement | Syntax | Brief Description |
| ;
|
;
|
Empty statement. Do nothing.
|
| break | break label | Jump to the designated label. |
| case | case expression: | Define a case block inside a switch statement. |
| cast | variable cast Type | Cast the variable to the specified type. |
| continue | contine | Continue the loop at the top of the loop. |
| catch | catch (error) { body } | Catch an exception. |
| class | class className { body } | Define a new class. |
| default | default: | Begins the default case for a switch statement. |
| delete | delete property; | Delete the named property (or variable). |
| do | do { body } while | Do / while statement |
| finally | finally { body } | Define a try / finally code block that is always executed. |
| for
|
for (init; condition; increment)
statement |
Standard for loop.
|
| for (.. in
|
for (variable in object)
statement |
Iterate over all property names in an object.
|
| for each (.. in | for each (variable in object)
statement |
Iterate over all properties in an object. |
| function
|
function [get | set] name([arg1 [... , arg2])
{ statements } |
Define a function.
|
| if / else
|
if (expression)
statement |
Conditionally execute a statement.
|
| include | include "filename" | Include the script file. |
| interface | interface Name { body } | Define a type interface. |
| let | let identifier [ = value ] [... , identifier [ = value]];
|
Declare and initialize block scope variables.
|
| new | new ClassName(args) | Create a new object instance based upon a class. |
| return
|
return [expression];
|
Return a value from a function.
|
| super | super(args) | Invoke the super constructor. Also used to refer to the super type. |
| switch | switch (expression) { body } | Multi-case conditional switch. |
| throw | throw new ExceptionClass() | Throw an exception. |
| try | try { body } | Execute code within an exception try / catch handler. |
| use | use pragmas | Define program pragmas. |
| var
|
var identifier [ = value ] [... , identifier [ = value]];
|
Declare and initialize variables.
|
for
The for statement provides the basic looping construct for EJS. Similar to the C for loop it implements an initialization, conditional and increment phases of the statement. The for statement also provides a for / in construct that is described below.for (initialization; conditional; increment)
statement;
For example:
for (var i = 0; i < 10; i++) {
print("i is " + i);
}
You can put any expression or statement in the initialization or increment sections. In this example, we simply define a variable i and initialize it to zero in the initialization section and increment it each time after the statement body is executed. The conditional expression must evaluate to a boolean value which if true allows the statement body to be executed. The increment expression is evaluated after the statement body is executed and before the conditional is retested.
for .. in
The for statement has a powerful variant that allows you to iterate over all the property names in an object. It uses the following syntax:for (variable in object)
statement;
This statement will execute the statement body for each property name in the object. Each time, the variable will be set to the name of the next property in the object. The order of the walk through all the properties is determined by the order of property creation. NOTE: it will not be set to the property value, but will be set to the property name. For example:
for (var v in customer) {
println("customer." + v + " = " + customer[v]);
}
This will print "customer.propertyName = value" for each property defined in the customer
object.
for each .. in
The for each statement allows you to iterate over all the properties in an object. It uses the following syntax:for each (variable in object)
statement;
This statement will execute the statement body for each property in the object. Each time, the variable will be set to the next property in the object. The order of the walk through all the properties is according to the order of property creation.
for (var v in customer) {
println(v);
}
This will print the value of each property defined in the customer object.
function
The function statement defines a new global function according to the syntax:
function name([arg1 [... , arg2]) {
statements
}
The function name must be an identifier and not a reserved word and is followed by an optional list of arguments. Between braces, a set of statements define the function body. For example:
function min(arg1, arg2) {
if (arg1 < arg2) {
return arg1;
} else {
return arg2;
}
}
Function declarations can also be nested, i.e. a function may be defined in the statement body within an outer function. In this manner, the inner function will only be visible within the scope of the outer function.
When the function is invoked a new local variable store is created so that any variables declared and used in the function will be private to the function. Functions invoke other functions and each function will have its own local variables. If a variable is assigned to without using the var statement, the variable will be created in the global variable store.
if / else
The if statement is the primary conditional execution ability with Ejscript. It takes the form:if (expression)
statement
[ else statement ]
The expression is evaluated and if true, the first statement is executed. If an else phrase is added and the expression evaluates to false, then the else statement will be executed.
Statements may be grouped using braces. For example:
if (i < j) {
println("i is " + i);
println("j is " + j);
} else {
// Do something
}
The conditional expression may be a compound conditional expression. For example:
i = 0;
j = 1;
if (i < j || j != 0 || getToday() == "sunday") {
// Do something
}
EJS uses lazy evaluation where if the first expression (i < j) is true, then the following expressions will not be evaluated. In the example above, getToday() will not be called as i is less than j.
Switch
You can chain if / else statements together to implement a switch capability. For example:
if (today == "monday") {
// Do something
} else if (today == "tuesday") {
...
} else {
// Default
}
return
The return statement is used to supply a return value inside a function. The return statement may appear without an expression to cause the function's execution to terminate and return.A return expression may be a simple value or it may be an object reference. For example:
function myFunc(x)
{
if (x == 0) {
return null;
}
return new MyObj(x);
}
var
The var statement declares variables and initializes their values. Although not strictly required by the language, as variables will be declared automatically by simply assigning to them, it is good practice to always use var declarations for all variables. The var statement takes the form:var identifier [ = value ] [... , identifier [ = value]];
For example:
var x;
var y = 4;
var a, b = 2, c = "sunny day";
If an initializer value is not defined, the identifier will be set to the undefined value.
If the var statement is used within a function, the variable is defined in the local variable store. If it is used outside a function, the variable is defined on the global store. If a variable is assigned to without a var statement, then the variable is created on the global store. For example:
x = 2;
function myFunc()
{
x = 7;
}
println("x is " + x);
This code snippit will print "x is 7".