Introduction to JavaScript Execution process

Source: Internet
Author: User
Tags reference

As we know, when we write the JS program, open the browser, our code can start to run (of course, to ensure that your code is not a problem, in order to do as you expected). Just said is JS implementation of a large environment, today we learn, JS in the parser in a process of implementation.

This process is divided into two stages:

Enter the execution context to execute the change of the code variable object, and these two phases are closely related.

Before you introduce these two phases, understand the relevant concepts.

If the variable is related to the execution context, it should know where to store the data and how to access the data, which is called the variable object (variable object, or VO). Used to store the following data:

Variable declaration function declaration (this place needs to be distinguished from function expressions) function arguments

First stage: Enter execution context. Vo will then be initialized by the following attribute (initialized in the following order):

Function parameter: A property of Vo that is a physical parameter name and a value composition, and if no argument is passed, it is the name and undefined of the formal parameter. function declaration: is composed of the name and value of the function, if the attribute value exists in Vo, replace this property. Variable declaration: A variable name and undefined, and if the variable name and VO have the same function argument or function declaration, the variable declaration does not interfere with the existing attribute.

VO's use environment is: Globalcontext and Functioncontext. Here's an example:

function Test (A, b) {  
      var c =;  
      Function d () {}  
      var e = function _e () {};  
     (function X () {});  
}  
Test (30);

When entering the execution environment, VO is as follows:

VO (Test Functioncontext) = {  
       a:30,  
       b:undefined,  
       D: <reference to Functiondeclaration "D",  
       C:un deifined,  
       e:undefined  
   }

Note: where x and _e are function expressions, _e is accessed through variable declaration E.

Next you go to the next stage, executing the code phase:

In the example above, you will experience the following procedure:

Vo[' c '] =;  
Vo[' e '] = <reference to Functiondeclaration "_e" >;

So the code is done.

Here's another classic example:

alert (x); function  
var x = ten;  
x =;  
function X () {}  
alert (x);    20

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

Why is the first function, not undeofined or not defined or 10, 20? Because, according to the specification-when entering the context, go to Vo to fill in the function declaration; At the same stage, there is also a variable declaring "X", so as we said in the previous stage, the variable declaration follows the function declaration and the formal parameter declaration, and, at this stage, A variable declaration does not interfere with a function declaration or formal parameter declaration that already exists in Vo, and therefore, when entering the context, the structure of VO is as follows:

VO = {}  
vo[' x '] = <reference to Functiondeclaration "x" >;  
vo[' x '] = <the value is not disturbed, still function>

In the code execution phase:

v[' x '] = ten;  
v[' x '] = 20;

Understand this process, I believe that the JS implementation process will have a new understanding.

Leave a small question (guess the output):

function Test (A, b) {  
      var c =;  
      Console.log (a);//What is the result? Why?  
      function A () {}  
      var e = function _e () {};  
     (function X () {});  
}  
Test (30);
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.