Javascript execution environment (execution context) and scope and garbage collection

Created At: 2023-01-14 08:28:15 Updated At: 2023-01-14 23:00:27

The execution environment is divided into the global execution environment and the function execution environment. Every time a new execution environment is entered, a scope chain for searching variables and functions will be created.

The local environment of a function not only has access to the variables the function acts on, but also has access to its outer environment, up to the global environment.

The global execution environment can only access variables and functions of the global execution environment, and cannot directly access information in the local environment; the execution environment of variables helps determine when memory should be freed. Values ​​that go out of scope are marked as reclaimable and will be deleted during garbage collection. There are two garbage collection algorithms in javascript, "mark clear" and "reference counting".

Execution Environment

Execution environment is a very important concept in javascript. The execution context of a variable or function defines what other data they have access to, as well as the respective behavior of the variable or function.

Each execution environment has a variable object associated with it, and all variables and functions defined in the environment are stored in this object. The code we write doesn't have access to this object, but the parser uses it behind the scenes when processing data.

The global execution environment is the most peripheral execution environment. Depending on the host environment where ECMAScript is implemented, the objects representing the execution environment are also different.

In the browser, the global execution environment is considered as the window object, and all global variables and functions exist as properties or methods of the window object.

Once the code in an execution environment is executed, the environment will be destroyed, and the variables and functions stored in it will be destroyed accordingly.

Each function has its own execution environment. The global execution environment will not be destroyed until the application exits, such as closing the web page.

Scope Chain

When code is executed in an environment, a scope chain of variable objects is created. The role of the scope chain is to guarantee ordered access to all variables and functions that the execution environment has access to. The scope chain can be regarded as an organic and orderly connection of variable objects.

The first object of the scope chain is always the variable object of the environment where the currently executing code is located.

The next object in the scope chain comes from its outer environment, and the next object in the next outer environment, and so on up to the global execution environment.

The last object in the scope chain is always the variable object of the global execution environment. If the execution environment is a function, its activation object is used as the variable object. The activity object initially contains only one variable, the arguments object (which does not exist in the global environment).

Identifier parsing is the process of searching for identifiers along the scope chain level by level, until the identifier is found, and if the identifier is not found, an error will occur. As follows

var name = "Dastagir Ahmed";

function changeName()
{
    var newName = "Dylan Ahmed";
    
    function execute(){
        var tempName = newName;
        newName = name;
        name = tempName;
        
        // can access name / newName / tempName
    }
    execute();
    // can access name / newName , can not access tempName
}
changeName();
// can only access name

The example involves 3 execution environments:

  1. the global environment,
  2. the local environment for changeName(), and
  3. the local environment for execute()

The inner environment can access all the outer environment through the scope chain, but the outer environment cannot access any variables and functions in the inner environment.

The connection between these environments is linear and orderly. Each environment can search up the scope to look up variable and function names; but neither can search down the scope chain into another execution environment

Scope chain extension

Although there are two types of execution environments: the global execution environment and the local execution environment (function), there are still some statements that will temporarily add a variable object to the front of the scope chain, and the variable object will be removed after the code is executed. This behavior occurs in two cases, and the scope chain is lengthened when execution flow enters any of the following statements:

  1. catch block
  2. with statement
  3. no block scope

JavaScript does not have block scope. In other C-like languages, the code enclosed by curly braces has its own scope, which can realize the definition of variables based on conditions.

Comment

Add Reviews