Embedded JavaScript is case sensitive and uses ASCII as its
character set. White space between tokens is ignored with the
exception that statements that omit trailing semicolons must be
on a line by themselves.
Comments can use either the C commenting style with text between
/* */ delimiters, or the C++ style may be used where text from //
to the end of the line is regarded as a comment.
Identifiers are names used for variables and functions.
Identifiers must begin with either a letter, and underscore '_'
or a dollar sign. Identifiers must not use equal any reserved
word from the table below:
Reserved
Words
Embedded JavaScript regards all these words as
reserved.
delete
|
else
|
false
|
for
|
function
|
if
|
in
|
new
|
null
|
return
|
this
|
true
|
var
|
|
|
|
Embedded JavaScript also regards these words as reserved. They
are not yet implemented, but users should avoid them for future
compatibility.
break
|
case
|
catch
|
continue
|
default
|
do
|
finally
|
instanceof
|
switch
|
throw
|
try
|
typeof
|
void
|
while
|
with
|
|
It is also recommended that you do not use any of the following
words. ECMA may implement them in future standards
abstract
|
boolean
|
byte
|
char
|
class
|
const
|
debugger
|
double
|
enum
|
export
|
extends
|
final
|
float
|
goto
|
implements
|
import
|
int
|
interface
|
long
|
native
|
package
|
private
|
protected
|
public
|
short
|
static
|
super
|
synchronized
|
throws
|
transient
|
volatile
|
|
Primitive
Data Types
Variables are declared either by the
var keyword or by simply assigning a value to a new
variable name. In JavaScript is is not essential to declare
variables before using them. However, it is considered good
practice to do so.
JavaScript does not have explicit type declarations. They are not
given an explicit type when declared. The underling type of a
variable is determined by the type of the data stored in the
variable. For example:
var x;
x = "Hello World";
This will cause the variable
x to store
the string "Hello World". Embedded JavaScript will store this as
the primitive String data type. If this variable is assigned an
integer, the underlying type will be changed. For example:
x = 55;
The
x variable will not hold an integer
primitive type with a value of 55. If
x
is used in expressions, its value will be copied and converted to
other types as required.
Embedded JavaScript supports the following primitive data types.
These types can be combined in objects to create arbitrary
structured data types.
- Boolean
- Floating Point
- Integer
- 64-bit Integer
- Strings
JavaScript will automatically convert variables from one
type to another as required by the current expression.
Numeric Type
The ECMA standard specifies that JavaScript
numerics are stored and manipulated as floating point entities.
For embedded uses, this is often less than ideal. Consequently,
EJS allows the default numeric type to be selected at build time.
Binary releases typically select 32-bit integer as the default
numeric type. Note that floating point can still be used and
expressions can still be explicitly converted to floating point
by multiplying by 1.0.
Boolean Type
A boolean value can be either true or
false. Assigning either true or false to a variable or the result
of a conditional expression will set the type of a variable to
boolean. The following example will set x to true.
var y = 1;
var x = (y == 1);
EJS defines the global variables
true and
false with the appropriate boolean
values.
Integer Type
The default numeric type for is a 32-bit
integer and integer literals are 32-bit integers. Integer
literals may be specified as octal values by prefixing with a '0'
or hexadecimal by prefixing with 0x or 0X.
64-bit integers are also supported if the EJS is built with the
default numeric type set to be 64-bit integers.
Floating Point Type
Floating point numbers are stored as
C doubles. Floating point literals may be specified simply by
using a decimal point in the literal. For example:
var pi = 3.14;
Floating point literals can also use an exponential notation of
the form: floating number followed by 'e' or 'E', followed by an
optional sign '+' or '-', followed by an integer exponent.
I.e.
[digits] [.digits] [E|e] [(+|-)digits]
For example:
var pi = 3.14e1;
var pi = 314.0e-2;
Embedded JavaScript also defines two useful floating point
constants. The global variable
NaN is set
to the "not a number" value. This value is the result when a math
operation gives an undefined value or error. For example, divide
by zero will result in the NaN value.
The other constant is
Infinity. If a
floating point operation overflows the largest possible floating
number, the result is equal to the Infinity value.
String Type
JavaScript strings are immutable sequences of
ASCII letters or escaped literals. They are primitive types and
are not treated as objects. This means that they are always
passed by value and not by reference. String literals may be
enclosed in either single or double quotes.
JavaScript strings are easy to work with due to the inbuilt
ability to append strings via the "+" operator. For
example:
var str = "Happy Birthday " + name;
EJS supports the following escaped letters inside string
literals:
Escape Sequence
|
Description
|
\b
|
Backspace (0x8)
|
\f
|
Form feed (0xc)
|
\n
|
New line (0xa)
|
\r
|
Carriage return (0xd)
|
\t
|
Horizontal tab (0x9)
|
\v
|
Vertical tab (0xb)
|
\uNNNN
|
Unicode character (0xNNNN)
|
\xNN
|
Hexadecimal characgter (0xNN)
|
\NNN
|
Octal character (ONNN)
|
\'
|
Literal single quote
|
\"
|
Literal double quote
|
\\
|
Literal backslash
|
Functions
A JavaScript function is a JavaScript that can
be executed by name with supplied arguments. Functions are
declared via the
function keyword
followed by optional arguments and a function body contained
within braces. For example:
function myPoint(x, y)
{
return x + y;
}
Functions can use a
return statement to
return an arbitrary value value. This may be a primitive type or
it may be a reference to an object.
To invoke a function, the function name is used with the ()
operator following. The actual arguments are supplied within the
"()" . The arguments to the function are evaluated left to right.
For example:
var x = myPoint(1, y + 7);
Functions may also be assigned to properties of objects to create
methods.
Scope and the Function Call Object
When functions are
invoked, EJS creates a new set of local variables for use by the
function. EJS also creates several objects to describe the
function call.
The function call arguments are passed in as named parameters and
they are also stored in the
arguments[]
array. The arguments array allows functions to be written to take
a variable number of arguments. For example:
function max()
{
biggest = 0;
for (i = 0; i < arguments.length; i++) {
if (arguments[i] > biggest) {
biggest = arguments[i];
}
}
}
EJS also defines a
callee object property
in the arguments object. The callee property has a length
property member that stores the expected number of arguments by
the function. For example:
function myFunc(x, y)
{
if (arguments.callee.length != 2) {
// Error
}
}
NOTE: JavaScript and EJS do not validate the number of arguments
supplied to a function at run-time.
Embedded JavaScript does not implement a few capabilities from
the ECMA specification relating to functions. These include
nested functions, the prototype property and the apply and call
methods.
Variables
Variable Scope
JavaScript defines two scopes for
variables: global and local. Global variables are shared among
functions that execute in a single JavaScript program. Local
variables are created for each function and are destroyed on
function exit. Objects are created on an object heap and
references to the objects may be stored in either the global or
local variable stores.
As described earlier, JavaScript variables may be optionally
declared via the
var statement. When used
inside a function, the variable is created in the local variable
store. If used outside any function, the variable is created in
the global store.
If a variable is assigned to without a prior var statement, then
the variable is always created on the global store.
Undefined and Null
If a variable is declared but not
assigned, its value is said to be undefined. For example:
var x;
This declares a variable x, but does not assign it a value. All
such unassigned variables are set to the
undefined value. Variables can be tested for equality
with undefined. For example:
if (x == undefined) {
// X does not yet have a value
}
Do not confuse undefined with
null. The
null value implies that a variables does not point to a valid
object. You can assign null to a variable to disconnect the
variable from pointing to an object.
Type
Conversions
JavaScript will usually handle type conversions
for you automatically. Adding a numeric to a string will cause
the number to be converted to a string and appended to the left
hand side string. Adding an integer to a floating point will
cause the integer to be converted to a floating point prior to
the arithmetic.
If you want to explicitly force a type conversion, use the
following
Initial Type
|
Target Type
|
Action
|
Example
|
Integer
|
String
|
Add a string on the right
|
num + "";
|
String
|
Integer
|
Add a numeric on the right
|
1 + string;
|
Integer
|
Floating Point
|
Mulitply by 1.0
|
num * 1.0
|
Object
|
String
|
Invoke the toString method
|
o.toString()
|
Type Conversion Rules
Embedded JavaScript observes the
following type conversion rules:
- If the left operand is a string, the right hand operand
will be converted to a string. If both operands are numeric, a
numeric operation is done.
- If either operand is an object, it is converted to a
string.
- If both operands are boolean and the operation is ==, !=,
then proceed without a type conversion.
- If one operand is a string and the other is a numeric, try
to convert the string to a number. Otherwise, try to convert
the numeric to a string.
- If both are numeric, but not of equal types. Give priority
to floating point, then 64-bit integer, then integer, then
boolean.