Main Page

interesting aspect of ECMAScript

?
Pascal Notation — the first letter is uppercase and each appended word begins with an upper-
case letter. For example:
var MyTestValue = 0, MySecondTestValue = “hi”;
?
Hungarian Type Notation — prepends a lowercase letter (or sequence of lowercase letters) to
the beginning of a Pascal Notation variable name to indicate the type of the variable. For exam-
ple,
i
means integer and
s
means string in the following line:
var iMyTestValue = 0, sMySecondTestValue = “hi”;
The following table list prefixes for defining ECMAScript variables with Hungarian Type Notation.
These prefixes are used throughout the book to make sample code easier to read:
Type
Prefix
Example
Array
a
aValues
Boolean
b
bFound
Float (Number)
f
fValue
Function
fn
fnMethod
Integer (Number)
i
iValue
Object
o
oType
Regular Expression
re
rePattern
String
s
sValue
Variant (can be any type)
v
vValue
Another interesting aspect of ECMAScript (and a major difference from most programming languages)
is that variables don’t have to be declared before being used. For example:
var sTest = “hello “;
sTest2 = sTest + “world”;
alert(sTest2); //outputs “hello world”
In the previous code,
sTest
is declared with a string value of
“hello”
. The next line uses a variable
named
sTest2
to create a concatenation of
sTest
and the string
“world”
. The variable
sTest2
hasn’t
been defined using the
var
operator; it has just been inserted as if it has already been declared.
When the ECMAScript interpreter sees an identifier that hasn’t been declared, it creates a global variable
with the given name of the identifier and initializes it with the value specified. This is a handy feature of
the language, but it can also be dangerous if you don’t keep track of variables closely. Best practice is
always to declare all variables as you would with other programming languages (for more information
on why you should always declare variables, see Chapter 19, “Deployment Issues”).
14
Chapter 2
05_579088 ch02.qxd 3/28/05 11:35 AM Page 14


JavaScript EditorFree JavaScript Editor     Ajax Editor


©

R7