Main Page

Summary

return iNum1 + iNum2 + iBaseNum;
}
return doAddition();
}
Here, the function
addNumbers()
contains a function (the closure) named
doAddition()
. The internal
function is a closure because it captures the arguments of the outer function,
iNum1
and
iNum2
, as well
as the global variable
iBaseNum
. The last step of
addNumbers()
calls the inner function, which adds the
two arguments and the global variable and returns the value. The important concept to grasp here is that
doAddition()
doesn’t accept any arguments at all; the values it uses are captured from the execution
environment.
As you can see, closures are a very powerful, versatile part of ECMAScript that can be used to perform
complex calculations. Just as when you use any advanced functionality, exercise caution when using clo-
sures because they can get extremely complex.
Summary
This chapter looked at the basics of ECMAScript:
?
General syntax
?
Defining variables using the
var
keyword
?
Primitive and reference values
?
The basic primitive types (Undefined, Null, Boolean, Number, and String)
?
The basic reference types (Object, Boolean, Number, and String)
?
Operators and statements
?
Functions
Understanding ECMAScript is an important part of JavaScript programming, which is why this chapter
is perhaps the most important in this book. A good grasp of the core is vital to comprehending the rest of
the topics in the book.
The next chapter focuses on more of the object-oriented aspects of ECMAScript, including how to create
your own classes and how to establish inheritance.
66
Chapter 2
05_579088 ch02.qxd 3/28/05 11:35 AM Page 66


JavaScript EditorFree JavaScript Editor     Ajax Editor


©

R7