0

I read a lot about LexicalEnvironment, but haven't found how closure is implemented at this level.

Closure function keeps its LexicalEnvironment despite that ExecutionContext of outer function is deleted. Could you help me find out where this data is stored after outer function is executed and its context is deleted?

Any idea regarding FunctionObject, keeping several LexicalEnvironments simultaneously? Is it possible to illustrate closure like this:

GlobalExectionContext = {
  ThisBinding: <Global Object>,
  LexicalEnvironment: {
    EnvironmentRecord: {
      Type: "Object",
      a: < uninitialized >,
      b: < uninitialized >,
      multiply: < func >
    }
    outer: <null>
  },
  VariableEnvironment: {
    EnvironmentRecord: {
      Type: "Object",
      c: undefined,
    }
    outer: <null>
  }
}
FunctionExectionContext = {
 
  ThisBinding: <Global Object>,
  LexicalEnvironment: {
    EnvironmentRecord: {
      Type: "Declarative",
      Arguments: {0: 20, 1: 30, length: 2},
    },
    outer: <GlobalLexicalEnvironment>
  },
VariableEnvironment: {
    EnvironmentRecord: {
      Type: "Declarative",
      g: undefined
    },
    outer: <GlobalLexicalEnvironment>
  }
}

Thanks!

I read attentively ECMA Specification, watched youtube :) but the best what I found looked like this: "When outer function is executed its context will be deleted. At the same time its LexicalEnvironment is kept as inner (closure) function has access to its variables". or "Even though an execution context is deleted from the stack once it has run all the codes inside, a reference to the function is alive when the original function returns a new function that uses a variable out of its scope. Then, the link to the outer function won’t be deleted until its codes are executed and completely removed from the stack. This is called Closure. Closure is a function that allows you to access the parent’s function scope, even though its execution phase is finished." In Chrome I see Closure scope appearing during Debug.

  • *Could you help me find out where this data is stored after outer function is executed and its context is deleted?* <-- In the scope chain. Please see [this answer](https://stackoverflow.com/questions/41385835/invocation-context-and-execution-context-in-javascript-are-we-talking-of-th/41386097#41386097) that might help. – Scott Marcus Jan 03 '23 at 21:27
  • Please clarify what you mean by "*where this data is stored*". It's stored in the same place as before, it's simply not deleted when the execution context is popped from the call stack. This is basic garbage collection: it's kept around as long as it is needed. – Bergi Jan 04 '23 at 01:04
  • "*Is it possible to illustrate closure like this*" - which code (in which execution state) are you trying to illustrate by that? What do the values in angle brackets mean? – Bergi Jan 04 '23 at 01:06

0 Answers0