HTML and CSS Reference
In-Depth Information
Closures are another of the most important features of JavaScript. When we continue the
development of the sample web application we will make extensive use of closures.
Closures can be a difficult concept to explain, so it is useful to learn about them through ex-
Consider the following code:
> function f() {
var i = 0;
return ++i;
This example uses ++i rather than i++. This means that the value is incremented
(has 1 added to it with the ++ operator) before it is returned. If this had used i++,
the value would be returned and then incremented by 1 - which is not the same
This code defines a function f . Inside this function a private variable is declared and initial-
ised to 0. This variable is then incremented by 1 and returned.
If we perform repeated calls to this function, it is not surprising that it will always return 1:
> f()
> f()
> f()
Search WWH ::

Custom Search