0

We knew that

function foo () {
  var x = 10;
  var y = 20;
  function bar () {
    return x + 1;
  }
  bar(); // 11
}

The function bar create a closure, and save the reference of x.

But what about the variable y? Will the closure bar created hold its reference? I tried it in Chrome Dev Tools, and it shows only x in the [[Scopes]] field, without y. But I can not find any articles about that.

Does it means the closure creation will only pick what it need to save?

Narcotics
  • 313
  • 1
  • 8

1 Answers1

0

Scopes are chained together and it'll check with parent scope when reference cannot be find. This question is answered before and you can reference it here: Scope Chain in Javascript

Community
  • 1
  • 1