🔰lexical environment
JS ⟩ concept ⟩ execution context ⟩ lexical environment
In JavaScript, every running function, code block {...}, and the script as a whole have an internal associated object known as the "lexical environment", it consists of two parts:
environment record – an object that stores
all local variable(s) as its properties.
other information like the value of this.
A reference to the outer lexical environment (associated with the outer code).
📗 JS.info ⟩ Lexical Environment
// make counter
function Counter() {
let count = 0;
return function() {
return ++count;
};
}
// a counter instance
let counter = Counter();
counter(), // 1
counter(), // 2🔸environment record - stores local variables/this ...
🔸[[Environment]] - reference to lexical environment where a function was made.
⭐closure - function that can access its outer lexical environment.
❗modify current scope at runtime❗️ - (actually) modify current lexical environment.
⚖️scope chain is similar to lexical environments.
⭐lexical environment is part of execution context.
⭐closure closes over its outer lexical environment.
⭐declaration determines scope(s) at compile-time.
❗var (a statement)
Q:lexical environment === scope❓
Last updated
Was this helpful?