I have been looking at ecma262 and I've seen that when we invoke a function we are basically calling the internal method [[Call]]
and while I was looking at the steps of the [[Call]]
I have found that in step 2 it calls to PrepareForOrdinaryCall
and here is the interesting part.
- Let localEnv be NewFunctionEnvironment(F, newTarget).
- Set the LexicalEnvironment of calleeContext to localEnv.
- Set the VariableEnvironment of calleeContext to localEnv.
PrepareForOrdinaryCall
sets the LE and the VE to localEnv
which is a newly created FunctionEnvironmentRecord.
Therefore, the LE and VE are pointing to the same ER.
Take a look at the following code:
function test(){
let localVar = 5;
var myVar = 10;
}
test();
let localVar
is going to be part of the FunctionEnvironmentRecord
which LE and VE are currently pointing to or it's going to create a new ER and set the LE to it and inside the new ER that LE pointing to it's going to store let localVar
Here some images to demonstrate it visually.
Example 1
is the let localVar is going to be part of the FunctionEnvironmentRecord which LE and VE are currently pointing to
Example 2
or it's going to create a new ER and set the LE to it and inside the new ER that LE pointing to it's going to store let localVar
Which one of those examples is the correct one?