Scope & Closures
Chapter 1: What is Scope?
Chapter 2: Lexical Scope
Chapter 3: Function vs. Block Scope
Scope From Functions
1 | function foo(a) { |
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 global scope:
1 | bar(); // fails |
However, all these identifiers (a, b, c, foo, and bar) are accessible inside of foo(..), and indeed also available inside of bar(..) (assuming there are no shadow identifier declarations inside bar(..)).