[In-depth JavaScript] 1. Core JavaScript concepts: execution environment and scope (Reading Notes)

Source: Internet
Author: User

Read chapter 4th and chapter 7 of JavaScript advanced programming.

 

1. Basic Concepts 1. What is the execution environment? (Execution context)

The execution environment defines other data that variables or functions have the right to access and determines their respective actions.

Personal perception:

The so-called execution environment refers to a special code (such as a function) That is executed in this "execution environment". Through the restrictions of this execution environment, determines what data this code (here a function) allows to access.

 

2. What is a variable object? (Variable object)

  Each execution environment has a variable object associated with it. All variables and functions defined in the environment are stored in this object.. (Although the code we wrote cannot access this object, the parser will use it in the background when processing data)

Personal perception:

In my opinion, the execution environment is a very abstract concept. In JS Code implementation, the abstract concept needs to be embodied. Therefore, an object associated with the execution environment is used, indicates the concrete existence of the environment.

 

3. What is a scope chain? (Scope chain)

  When the code is executed in an environment, a scope chain consisting of variable objects is created.. The purpose of the scope chain is to ensure that all variables and functions that are accessible to the execution environmentSequential access.

Personal perception:

As mentioned above, JavaScript determines the data that can be accessed by variables or functions through restrictions on the execution environment. Now, we use a linked list to store all the variable objects that can be accessed (the variable objects store accessible data) so that we can access the data in an orderly manner. (Why do you want to do this)

In addition, the scope chain is essentially a variable objectPointer listIt only references but does not actually contain variable objects.

 

4. What are activity objects? (Activation object)

  If the environment is a function, the activity object is used as the variable object.. At the beginning, the function's activity object only contains one variable, that is, the arguments object.

Personal perception:

According to my current narrow knowledge reserve, all I know is that all functions will use their own activity objects as variable objects, and then insert them into the front-end of the scope chain.

 

Ii. Execution Process 1. Key knowledge

Let's talk about some key things and then the process.

(1) first, the global execution environment is the most peripheral execution environment. In a Web browser, the global execution environment is considered a window object. Therefore, all global variables and functions are created as properties and methods of the window object.

(2) After all the code in an execution environment is executed, the environment will be destroyed and all the variables and function definitions stored in the environment will be destroyed. A variable object in a local environment such as a function exists only during function execution, and the global execution environment will not be destroyed until the application exits.

(3) then, each functionCalled for the first timeCreate your ownExecution EnvironmentAnd the correspondingScope chainAnd assign the scope chain to a special internal attribute.[[Scope]Then, use the value of this, arguments, and other named parameters to initialize the function's activity object. (All functions use their own activity objects as variable objects to represent their own execution environments)

(4) At last, there are some details about how to create a scope chain. Take the function as an example.Create a functionWill createAdvanceIt contains the Scope chain of the global variable object, which is saved in the internal [Scope] attribute. WhenCall a functionCreate an execution environment for the function, and thenCopyObjects in the [[Scope] attribute of the function construct the Scope chain of the execution environment. Then, an activity object (used as a variable object here) is created and pushed to the front end of the execution environment scope chain.

 

2. Execution Process

Let's start with the process.

  (1) Start code execution.// The initial environment is in the global execution environment

  (2) construct a scope chain and link it to a global object.// Create a scope chain for the window object. At this time, the element in the chain has only one object, which is the global execution object.

When the execution stream enters a function:

{

  (3) create a function execution environment.// Use the value of this, arguments, and other named parameters to initialize your own activity object.

// Use the activity object as a variable object to represent your own execution environment

  (4) construct a Scope chain and assign the chain header pointer to a special internal attribute [Scope] of the function.  

// The frontend of the scope chain is always a variable object in the environment where the code is currently executed

// The activity object contains only one variable at the beginning, namely, the arguments object (this object does not exist in the global environment)

// The next variable object in the scope chain comes from the include (external) environment, and the next variable object comes from the next include environment.

In this way, the variable objects in the global execution environment are always the last objects in the scope chain.

  (5) Push the function environment into an environment stack.

  (6) execute the function code.

// If You Need To search for identifiers in the middle of the process, search for identifiers at the first level along the scope chain. This is the purpose of the scope chain.

  (7) after the function is executed, the stack pops up its environment and the control is returned to the previous execution environment.    

// That is, the previous execution environment of the function execution environment in the scope chain

  (8) destroy the function environment.                       

// Destroy the variable object of the function, and destroy all the variables and function definitions stored in it.

}

  (9) execute other code and destroy the global execution environment when exiting the program.  

 

Okay, I admit that I wrote it too abstract. Let's take an example to understand it:

Var color = "blue"; function changeColor () {var anotherColor = "red"; function swapColors () {var tempColor = anotherColor; anotherColor = color; color = tempColor; // here you can access color, anotherColor, and tempColor
} // Here you can access color and anotherColor, but you cannot access tempColor swapColors ();
} ChangeColor (); // anotherColor and tempColor cannot be accessed here, But coloralert ("Color is now" + color) can be accessed );

Take the swapColors function as an example: (the scope chain construction of the changeColor function is similar to this)

When the execution flow enters the swapColors function, a pointer linked list is maintained in the function, that is, the scope chain.

 

Through this scope chain, a function hasOrderedWhileAccurate. When a function needs to read and write a variable, it first looks for whether the variable exists in the swapColors Scope (variable object) at the front of the linked list. If yes, the search stops and no longer looks up, if not, search up the scope chain.

We can see that:

  The connections between these environments are linear and ordered. In each environment, you can search for the scope chain to query the variables and function names. However, no environment can search for the scope chain to enter another execution environment.

 

Iii. Summary

The role of the scope chain lies in two important aspects. The first is the identifier Resolution, and the second is the closure.

Identifier Parsing is the process of searching for identifiers at the level of the scope chain. The search process always starts from the front end of the scope chain, and then goes back step by step until the identifier is found. If no identifier is found, an error is returned.

The closure also needs to use this scope chain. Next article will elaborate on closures.

It is not easy to concentrate on reading a large article. Thank you ~

Mutual encouragement.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.