Diving further into scope, we have the idea of scope chains and scope trees. Let's consider the following example:
function someFunc() {
let outerVar = 1;
function zip() {
let innerVar = 2;
}
}
What variable(s) does someFunc have access to? What does zip have access to? If you guessed that someFunc has access to outerVar but zip has access to both innerVar and outerVar, you're correct. That's because both variables exist in the scope chain of zip, but only outerVar exists in the scope of someFunc. Clear as mud? Great. Let's look at some diagrams.
Take a look at the following code:
function someFunc() {
function zip() {
function foo() {
}
}
function quux() {
}
}
We can diagram a scope tree of our function from a top-down construction:
Figure 5.3 – Scope tree
What does this tell us? quux kind of lives off in its own little world inside someFunc. It would have access to someFunc's variables, but not to zip or foo...