JavaScript execution contexts

A new execution context is created for each function in JavaScript.

How many execution contexts are there in memory when executing the following code? Note that the Bar function is not called.

 function Foo () { function Bar() {} } Foo(); 

Also, when are execution contexts created? During evaluation or lead time?

+6
source share
4 answers

Calling the run function at run time creates a run script. Therefore, in your example there is only one function call, therefore only one execution context is involved.

Static (compilation time) functions are important because they define the scope and the final contents of execution contexts. This is the actual function call, which is important, however, to create context. (Some older languages ​​used the term "activation record", although it might have been more intended for stack-based distribution.)

You can read the details in the sometimes foggy specification language, although it can be difficult to see the forest for trees. The spectrum is recorded from the point of view of the transmitted control. Calling a function is a very common way this happens, but it is also calling an event handler or calling the full <script> block when it is initially loaded by the browser.

+4
source

When creating a function, they are compiled at runtime. The function is called when you call them.

Let me clarify a bit:

You can have any number of function contexts, and each function calls creates a new context.

 //Global Context var helloWorld = "hello world!"; function foo(){//execution context function bar(){//execution context console.log('bar'); } } foo(); 

So, in the above code function, foo is called and creates a new context for the foo function, and the execution context for the bar is created only after it is called, but it has been compiled for the duration of the execution.

When a script loads for the first time by a browser, it enters the global execution context by default. If you call a function in the global scope, the sequence flow of your program enters the called function, creating a new execution context and dragging that context to the top of the execution stack.

Now let's talk in detail about the execution context:

So, every time a function is called, a new execution context is created. Each call to the execution context has 2 stages:

1. stage of creation:

When a function is called, but before it executes any code inside, do the following: create a chain of scopes, create variables, functions and arguments, and define the value "this".

2. stage of activation:

Assigning values, function references, and code execution.


Now let's find out a little about the execution context :

 function foo (a, b, c) { function z(){alert('Z!');} var d = 3; } foo('foo','bar'); 

ExecutionContext in a call to foo (): Step 1: arguments are created

 ExecutionContext: { arguments: { 0: 'foo', 1: 'bar', length: 2, callee: function() //Points to foo function } } 

Step 3a: creating a variable, arguments

 ExecutionContext: { arguments: { 0: 'foo', 1: 'bar', length: 2, callee: function() //Points to foo function }, a: 'foo', b: 'bar', c: undefined } 

Step 3b: creating a variable, function

 ExecutionContext: { arguments: { 0: 'foo', 1: 'bar', length: 2, callee: function() //Points to foo function }, a: 'foo', b: 'bar', c: undefined, z: function() //Created z() function } 

Step 3c: variable instance, variables

 ExecutionContext: { arguments: { 0: 'foo', 1: 'bar', length: 2, callee: function() //Points to foo function }, a: 'foo', b: 'bar', c: undefined, z: function(), //Created z() function, d: undefined } 

Step 4: set this value

 ExecutionContext: { arguments: { 0: 'foo', 1: 'bar', length: 2, callee: function() //Points to foo function }, a: 'foo', b: 'bar', c: undefined, z: function(), //Created z() function, d: undefined, this: window } 

After creating an ExecutionContext, the function runs its code from the first line until it finds a return or finishes work. Each time this code tries to access a variable, it is read from the ExecutionContext object.

+4
source

When calling Foo();

  • One execution context is created due to a call to one function
  • This process also creates a pointer to the Bar function.

In addition, there is such a default setting when your code is executed for the first time, called Global Context

+3
source

My best guess is that this may depend on the environment in which you use your code.

Although it is not very difficult to verify, V8 does not create an execution context for a function that is not executed at all.

Executed functionNon-executed function

+1
source

Source: https://habr.com/ru/post/978897/


All Articles