Willem van der Veen
Willem van der Veen

Reputation: 36680

[[scopes]] and the execution context

When we execute the following code

var addTo = (passed) =>{
    var add = (inner) => {
    var foo = () => {
        return inner + passed
    }
    return foo;
    }
  return add;
}


var addThree = addTo(3)(5);

console.dir(addThree);

We get the following in chrome devtools:

enter image description here

I understand what a closure is and why the first closure displays 5 and the second closure is displaying 3. I seem to get the concepts of closure and execution context (stack) now but don't know how they are exactly related.

  1. Is [[scopes]] just a representation of the scope chain?
  2. Is this at all related to the execution context?
  3. Is there a link between the execution context and closures?

Upvotes: 0

Views: 166

Answers (1)

Bergi
Bergi

Reputation: 665574

Is [[scopes]] just a representation of the scope chain?

Yes.

Is this at all related to the execution context?

Yes. When the function is called (like addThree()), the new execution context will create its local function scope to inherit from the parent [[scopes]].

Is there a link between the execution context and closures?

Nothing special. The currently active lexical environment, i.e. the (block) scope, is more relevant for the creation of closures.

Upvotes: 3

Related Questions