Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  30] [ 6]  / answers: 1 / hits: 43688  / 12 Years ago, wed, september 26, 2012, 12:00:00

Is lexical environment and scope in javascript one and the same thing?


More From » function

 Answers
14

Giving the answer based on what I have just learned from 'Secrets of the JavaScript Ninja, 2/e':


They are different concepts but related: we need to define a related concept - Execution Context & it's stack to understand.


Execution Context & Execution Context stack. Execution context is the internal JavaScript construct that tracks execution of a function or the global code. The JS engine maintains a stack data structure - execution context stack or call stack, which contains these contexts. The global execution context stays at the bottom of this stack. A new execution context is then created and pushed to the stack when execution of a function begins. A particular execution context tracks the pointer where statement of the corresponding function is being executed. An execution context is popped from the stack when the corresponding function's execution is finished.


Lexical Environment is the internal JS engine construct that holds identifier-variable mapping (here identifier refers to the name of variables/functions, and variable is the reference to actual object (including the function type object) or primitive value). A lexical environment also holds a reference to a parent lexical environment.


Now, for every execution context:



  1. a corresponding lexical environment is created and

  2. if any function is created in that execution context, reference to that lexical environment is stored at the internal property ([[Environment]]) of that function. So, every function tracks the lexical environment related to the execution context it was created in.


Every lexical environment tracks its parent lexical environment (that of parent execution context). As a result, every function has a chain of lexical environments attached to it.


Note: in JS a function is an object. Creating a function with a statement means creating an object of type Function. So, like other objects, a function can hold properties both internal and user defined.


Scope is a language agnostic concept that refers to the visibility of variables or functions to the executing code. In JS, a variable or function is visible to the executing code if it is there in the current lexical environment or in the lexical-environment-chain of the enclosing function. In case of global code, the chain does not exist.


Hope, you understand now...


Note: similar to the function, via the introduction of let and const in es6, when a block begins to execute (if block, for loop block etc), a new lexical environment is also created with the parent function's lexical environment as parent.


[#82905] Monday, September 24, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryderalfonsos

Total Points: 655
Total Questions: 88
Total Answers: 91

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
ryderalfonsos questions
Mon, Sep 9, 19, 00:00, 5 Years ago
Wed, Feb 13, 19, 00:00, 5 Years ago
Tue, Feb 12, 19, 00:00, 5 Years ago
Fri, Dec 28, 18, 00:00, 6 Years ago
;