In this section we will mention some execution contexts in JS, as well as executable code associated with the execution context.

Execution Context is an abstraction of the types and distinctions used for Exectuable code through the ECMA 262 specification. The standard does not technically define the type and structure of an Execution Context, which requires a JS engine to implement.

Logically, the execution context will form a stack, with the global context at the bottom of the stack and the current active Exection context at the top. The stack is modified each time the execution context is entered or exited.

1. Create a global execution context whenever an external JS file is loaded, or when the tag is entered; When the program starts,ECStack is: ECStack = [globalContext]

Each time a function is called, a functionContext is created, but it does not include the execution context of the internal function or the context of the recursive call. For example:

(function f(flag){
	if(flag){
		return;
	}
	f(true); }) (false);
Copy the code

Then the ECStack changes:

1. ECStack = [<f> functionContext globalContext] 2 when entering function f. ECStack = [<f> functionContext -> Recursive call <f> functionContext globalContext]Copy the code
Functions can be called repeatedly, so a function may have more than one execution contextfunctionfn(a){ } fn(1); fn(2); fn(3); All of the above are calls to the same function, but there are three execution contexts, and the three execution contexts are different. Arguments for variable objects are different, as explained in the next articleCopy the code

Each time a return is encountered or execution completes within a function, the current execution context exits from the stack, leaving the ECStack with the global context. So when does the global context exit from the stack? When you exit the entire program (close the file) the global context exits from the stack. Here’s an example of the above process:

var a = 10;
function sayHello(){
console.log('hello')
}
sayHello()
Copy the code

1. The global context is created as the first step into the JS code and pushed into the ECStack; Step 2 When the sayHello function is called, a sayHello function context is created and pushed into the ECStack. The diagram below:

Each execution context can be regarded as an object with specific attributes, including at least three values: Variable Object, Scope chain, and this Value.

The next article will look at Variab objects in execution context