Scopes and Closures

Initialization of functions and variables

  1. Instantiation of top-level variables
  2. Function variables
  3. Blocks do not have scope

Mechanics of variables and functions in JavaScript is completely different from most other languages.

Advanced topics become easy to grasp once you know how it works.

The "with" operator

  1. Lookup example
  2. Setting example
  3. Why with is deprecated?
  4. Summary

The with operator allows to use an arbitrary object as the scope.
It is used in the code around, but deprecated in modern JavaScript.


  1. Access to outer variables
  2. Nested functions
  3. Closures
    1. Mutability of LexicalEnvironment
    2. The notorious closure loop
    3. [[Scope]] for new Function
  4. Summary

From the previous article, we know that a variable is a property of the LexicalEnvironment object.

Here we discuss access to outer variables and nested functions. In-depth understanding of closures follows automatically.