Skip to main content

Functions

Functions in JavaScript are first class citizens.

When a function is created, the JavaScript engine first checks the syntax. After that:

  1. Allocated memory for a new function object.
  2. Attaches the function body, lexical environment map, etc
  3. Returns the function reference ID. This is like a pointer to where the function lives in memory.
functions not always recreated

Normal functions - the reference is created once, on first use. After that, the same reference is reused.

Child functions - the function object is created each time the parent runs. Every function has its own lexical environment. It's built during the run and garbage-collected after.