관리 메뉴

Silver Library (Archived)

Lexical environment? 본문

Fundamental of CS/Knowledge

Lexical environment?

Chesed Kim 2022. 11. 11. 19:19
반응형

https://www.intervue.io/developer/short-note/what-is-a-lexical-environment-sandbox-5XTCDNYWG

 

Intervue - Developer

Best short notes on programming concepts & questions asked in interviews

www.intervue.io

A lexical environment is a data structure that holds identifier-variable mapping. (here identifier refers to the name of variables/functions, and the variable is the reference to actual object [including function object or primitive value].

Lexical in general means in hierarchy or in a sequence.Whenever a new execution context(EC) is created a new lexical environment  is created and it is referenced in local EC in memory space.

Lexical Environment: Local Memory + Lexical Environment of its Parent

So in short, a lexical environment is a place where variables and functions live or physically present during the program execution.

 

// This is what a lexical environment conceptually look like:
LexicalEnvironment = {
  Identifier:  <value>,
  Identifier:  <function object>
}

//code example

function a() {
  var b = 10;
  c();
  function c() {
    console.log(b); //it prints the right value. How? See ans below Summary part
  }
 }
 
 a();
 console.log(b); // now when cursor comes here, it prints NOT DEFINED!

/*
function c is lexically inside function a. 
- So in EC of c(), variables and fun in c (none) + reference of lexical env of parent a() is there
- LE of a() in turn is its memory space + reference to LE of parent (Global EC)
- LE of Global EC points to its memory space + *NULL* (as no parent for Global EC).

*/

'Fundamental of CS > Knowledge' 카테고리의 다른 글

GraphQL (GQL) 에 대한 기록.  (0) 2022.11.21
What is possible and not : Next.js + some example  (0) 2022.11.13
Docker 사용의 기준점.  (0) 2022.11.05
What can I do with AWS amplify?  (0) 2022.11.05
What can I do with Firebase?  (0) 2022.11.05