Main Page

Variables

You will probably recognize some of these keywords as reserved words in ECMAScript Edition 3, which
is why they are reserved.
Additionally, the proposal asks for the following reserved words:
abstract debugger enum
goto
implements interface
native
protected synchronized throws transient
volatile
As you can see, this list of reserved words clearly shows the Netscape proposal moving ECMAScript
more towards a Java-like syntax.
The following words were reserved in ECMAScript Edition 3, but are used as part of the language in
Netscape’s proposal:
boolean byte char double final float int long short static
Finally, special meanings are attached to the words
get
and
set
, which cannot be used as variable names.
Variables
According to the Netscape proposal, variables can be defined with an explicit type by including a semi-
colon and the type name when defining the variable, like this:
var color : String = “red”;
In the previous code, a variable with name
color
is created as type
String
. Defining the type for a vari-
able is optional under this proposal. This can also be used with classes you would define yourself:
var specialObject : MyClass = new MyClass();
A slight twist on variables is the capability to create true constant values that cannot be changed by
using the
const
keyword:
const age = 32;
This code defines the variable
age
as a constant and assigns a value of
32
.
Functions
Functions also take advantage of this new type declaration by providing types for its arguments and
return value. Consider the following example:
function sum(num1 : Integer, num2 : Integer) : Integer {
return num1 + num2;
}
For compatibility with future ECMAScript implementations, you should avoid
using these words as variable or function names.
599
The Evolution of JavaScript
23_579088 ch20.qxd 3/28/05 11:44 AM Page 599


JavaScript EditorFree JavaScript Editor     Ajax Editor


©