0
  1. I'm a little confused about the timing when a Function gets its closures. Does that happends during the compile time or runtime. It would be better to explain that concept with the execuation context.

  2. Do all Functions born with the internal property [[scopes]]. If so, what is the relationship between [[scopes]] and the "reference to the outer lexical environment". Who determines the scopes chain?

The following code can be used as an example:

function a(){
    let t = 0;
    return function b() {
        t++
        console.log(t);
    }
}
let c = a();
c();

I'm trying to fully understand the relationship between execuation context, execuation stack, lexical environment, closure, [[scope]].

jcalz
  • 264,269
  • 27
  • 359
  • 360
江定玺
  • 19
  • 1
  • Related [When closures are created in JavaScript](https://stackoverflow.com/q/59844428). Disclaimer, I have an answer there. – VLAZ Apr 04 '23 at 06:12
  • 1
    It's notable, that [\[\[Scopes\]\]](https://stackoverflow.com/a/45508961/1169519) is not an internal JS property, it's a property in Chromium debugger only. – Teemu Apr 04 '23 at 09:16
  • 1
    I'm untagging "TypeScript" from here since the question doesn't ask about or even mention anything TypeScript-specific. – jcalz Apr 04 '23 at 12:57
  • 1
    @Teemu The EcmaScript standard has an [[OuterEnv]] internal property on functions which amounts to the same – Bergi Apr 04 '23 at 13:37
  • 1
    Very related: [Is it true that every function in JavaScript is a closure?](https://stackoverflow.com/q/30252621/1048572) – Bergi Apr 04 '23 at 13:40

0 Answers0