Context
I have this code
var a = 10;
// function "outer" created a closure of the global scope
// the moment it was defined
function outer() {
// variable a is available in this scope (execution context)
const b = 10;
// function "inner" created a closure of the global scope
// and outer function's scope the moment it was defined.
// When it's executed it can use this closure as part of
// its own scope
function inner() {
// variables a and b are available in this scope
const c = 20;
console.log(a + b + c);
}
return inner;
}
The closure for inner
has these scopes:
- Local scope
- Outer function's scope
- Global scope
Let's execute outer
and then execute the return value, which is inner
:
const inner = outer();
inner() // prints 40
Now here is where I am confused. I always thought that the inner
function gets a "snapshot" of variables a
and b
as they existed the moment inner
was defined.
So, I thought that after const inner = outer();
, then every execution of inner
would always return 40
.
However I came to find out (while making a demonstration nonetheless) that this is not what happens
const inner = outer();
a = 100;
inner() // prints 130 while I expected for inner to be "locked" to 40
Question
I would like someone to clarify/correct my mental model of how closures work in js.
More specifically, I expected that the moment a function is defined, it gets a hold of all the outer scopes defined up to that moment and var
(or const
or let
) values stay "locked" to the values they had the moment the function (in this case function inner
) was defined.
However, I just showcased that altering the value of one of the variables referenced inside inner
actually changes its return value.
So, values are evaluated "live" when the inner
function gets executed instead of being a snapshot of what they were the moment inner
was defined?