Reputation: 253
I try to understand how closure is working under the hood. So I've read a lot of documentation and so many different websites.
Then I read this Variable Environment vs lexical environment
And in old documentation (2011) https://262.ecma-international.org/5.1/#sec-10.2
there are two different things, LE and ER.
But in modern https://262.ecma-international.org/12.0/#sec-environment-records
There is only one, environment record, and description of it is very similar to the old describe of lexical environments/
So, I am really confusing. Does LE still exist for modern javaScript (ecmaScript 6+)? What the difference between LE and ER?
Upvotes: 2
Views: 749
Reputation: 445
Let's first go through the definition of Lexical Environment
& Environment Record
as per different versions of ECMAScript Specification.
From ES2015 till ES2020 Specification:-
Environment Record
Reference
to outer environmentConceptual look using pseudo-code:
context.environment = {
// storage
environmentRecord: {
<identifier> : <value>
<identifier> : <value>
}
// reference to the parent environment
outer: <...>
}
Note: - The [[Environment]]
created inside Execution Context is of type Lexical Environment[refer ES2020]
According to 12th Edition ECMAScript2021 Specification:
[[OuterEnv]]
field, which is either null or a reference to an outer Environment Record.Conceptual look using pseudo-code:
context.environment = {
// storage
<identifier> : <value>
<identifier> : <value>
// reference to the parent environment
outer: <...>
}
Note: - The [[Environment]]
created inside Execution Context is of type Environment Record[refer ES2021]
Let's also understand the Structure of execution context
LexicalEnvironment
, VariableEnvironment
, etc.In pseudo-code:
ExecutionContext = {
VariableEnvironment: { ... },
LexicalEnvironment: { ... },
// other components
}
Note:
Till ES2020 | From ES2021 |
---|---|
- The LexicalEnvironment component and VariableEnvironment component of an execution context are always Lexical Environments[refer ES2020] |
- The LexicalEnvironment component and VariableEnvironment components of an execution context are always Environment Records[refer ES2021] |
So, Yes Lexical Environment
still exists for Modern JavaScript[ES2021] but now the [[Environment]] created is of type Environment Records
.
Upvotes: 2