0

When we have an inner function that access a variable x1 defined in the lexical environment of its outer function, how the JavaScript engines handle keeping the x1 in the memory ?

Do they keep the already created execution context with its lexical environment of the outer function or they create a new lexical environment and copy the variable binding into it then link it as the outer lexical environment for the lexical environment of the inner function and destroy the execution context with the lexical environment of the outer function.

Can we consider a closure as a Lexical Environment ?

  • The outer function is func1.
  • The inner function is func2.
  • The outer variable is x1 defined inside func1.

In the following code:

function func1() {

  var x1 = 1;

  return function func2() {

    var x2 = 2;

    return [x1, x2];

  }

}

func1()();
faressoft
  • 19,053
  • 44
  • 104
  • 146
  • 1
    From the [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures): "A closure is the combination of a function and the lexical environment within which that function was declared." – Sterling Archer Oct 01 '17 at 18:29
  • I'm not sure what you mean by "*copy the variable binding into it*". – Bergi Oct 01 '17 at 18:33
  • @Bergi, Do JavaScript's engines really keep all these environments instead of destroying them ! How is that possible because they are part of the their execution context which must be removed from the call stack after they executed ! right ! I have an assumption that what actually happens is that the engines create an object called a closure which a lexical environment to contain a references to the variables of the outer lexical scope and associate that to the [[scope]] of the function. Do you think this is a right assumption ! – faressoft Oct 01 '17 at 19:13
  • 1
    @faressoft Yes, the closure object does have a reference (the [[scope]]) to the lexical scope it was declared in. If a lexical environment is referenced by a closure, it must indeed be kept until it can be [garbage-collected](https://stackoverflow.com/q/19798803/1048572) (and to be kept it's put on the heap, not on the stack). – Bergi Oct 01 '17 at 19:20
  • @Bergi really great, so the lexical environment doesn't garbage collected after destroying the execution context because the there is a closure references to it. Just one more thing, what references to what ? Do you mean the inner function stores a reference to the closure via the function's `[[scope]]` property, and the closure is an object that just has a reference to the lexical environment of the outer function ? – faressoft Oct 01 '17 at 19:44
  • 1
    @faressoft The function object *is* the closure – Bergi Oct 01 '17 at 20:04
  • @Bergi, thanks a lot, everything is cristal clear now :) You are genius. – faressoft Oct 01 '17 at 20:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155723/discussion-between-faressoft-and-bergi). – faressoft Oct 01 '17 at 20:15

0 Answers0