What is the execution context

There are many articles about the execution context on the Internet. Many articles are very clear about what is the execution context.
Let me talk about my understanding.
The execution context contains three things:

  1. VO
  2. this
  3. [[scope]]

Why include these three things? First we have to understand what is called the execution context.

The execution context is also called the execution environment. So, what is the environment?
Wikipedia explains this:

The environment refers to the surrounding things that are relative and related to a central thing .

In our code, for example, each function is a piece of executable code.
This executable code is the so-called central thing. The same function is executed in different places, and the output results are different. The surrounding things that lead to different output results are the so-called environment.

If we now have the following piece of code:

function doSomeThing(a){
	console.log(a);
	console.log(global);
	console.log(this);
}

Then, when we execute this code, it will first output the value of a, then the global object, and finally this.

However, this code is executed in different places, and the output is different.
For example, when our input parameter a is different, then the output a will be different. Therefore, a variable is an environment. All variables are collectively referred to as variable objects, also known as VO (variable object).
In addition, in different places, the direction of this is also different. This is obvious, and it is also a point of knowledge that our front end needs to focus on.
There is also [[scope]], which is the scope chain, linking to another execution environment pointer.

With these three things, each function can clearly know which variables it can access and what kind of environment it should be executed in.
This is why we call it the execution environment and execution context.

Guess you like

Origin www.cnblogs.com/edward-chenyu/p/12750990.html