Chapter 3. Function Versus Block Scope
As we explored in Chapter 2, scope consists of a series of âbubblesâ that each act as a container or bucket, in which identifiers (variables, functions) are declared. These bubbles nest neatly inside each other, and this nesting is defined at author time.
But what exactly makes a new bubble? Is it only the function? Can other structures in JavaScript create bubbles of scope?
Scope From Functions
The most common answer to those questions is that JavaScript has function-based scope. That is, each function you declare creates a bubble for itself, but no other structures create their own scope bubbles. As weâll see in just a little bit, this is not quite true.
But first, letâs explore function scope and its implications.
Consider this code:
function
foo
(
a
)
{
var
b
=
2
;
// some code
function
bar
()
{
// ...
}
// more code
var
c
=
3
;
}
In this snippet, the scope bubble for foo(..)
includes identifiers a
, b
, c
, and bar
. It doesnât matter where in the scope a declaration appears, the variable or function belongs to the containing scope bubble, regardless. Weâll explore how exactly that works in the next chapter.
bar(..)
has its own scope bubble. So does the global scope, which has just one identifier attached to it: foo
.
Because a
, b
, c
, and bar
all belong to the scope bubble of foo(..)
, they are not accessible outside of foo(..)
. That is, the following code would all result in ReferenceError
errors, as the identifiers are not available to the ...
Get You Don't Know JS: Scope & Closures now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.