Introduction to pre-parse (variable promotion) in JavaScript

  Today, Xiaoqian will introduce to you the pre-analysis (variable promotion) in JavaScript. Learn from what is pre-analysis and variable pre-analysis and function pre-analysis and loading process (note: we are talking about pre-analysis in ES5 here).

  What is parsing

  First of all, code execution definitely needs an execution environment, and the browser will provide a global scope window for javaScript execution. But before the javaScript is executed, it will be pre-parsed, which is also called variable promotion. Pre-analysis can be divided into two parts:

  -Declaration (declare): var a; Simply to understand the declaration is that we declare a variable without assignment;

  -Definition (defined): a = 100; Definition is equivalent to assigning a value to this variable;

  Before the javaScript is executed, the browser will pre-parse all var and function keywords in the global scope (note that the var and function keywords), which is why we learn pre-parse to learn the pre-parse of variables Reasons for analysis and pre-analysis of functions, there are certain differences between pre-analysis of variables and pre-analysis of functions as follows:

  -var: only declare but not define during pre-analysis

  -function: The declaration + definition have been completed at the time of pre-analysis

  Pre-analysis of variables

  Now there is the following code:


  This is why before we saw var a = 10; before we went to output a, it did not report an error but output undefined; declaring a variable without assignment is undefined;

  Function pre-analysis and loading process

  Earlier we said that when the function is pre-parsed, the declaration + definition have been completed, and now we have the following code:


  Let's first look at the loading process of the function:

  1. When the function is pre-parsed, the declaration + definition has been completed

  2. Because the function is a reference data type, a new heap memory space will be opened, the function will be stored in the form of a string, and a memory address such as xxxfff000 will be allocated to this heap memory space

  3. When the function is called, a new private scope will be opened, and the contents of the function body will be executed sequentially from top to bottom

  4. Each call of the function is independent of each other, and automatically destroyed after the function is executed

  It is convenient for everyone to understand the loading process as shown in the figure:


  From the figure above, it can be analyzed that the function is advanced and pre-analyzed. The declaration + definition has been completed during the pre-analysis, and a heap memory space is opened up to store the function in the form of a string, and this heap memory is allocated A memory address is convenient for us to find it when we use it, because at this time the function is stored in the form of a string, which is why the function definition function cannot be executed without calling it.

  When we call, we find that a new private scope is opened. The function body is executed from top to bottom, and each call will form a new private scope, so they are independent of each other, and each private function is executed. It is automatically destroyed, which is the garbage collection mechanism built into the browser. This ensures performance optimization.

  This article is from Qianfeng Education , please indicate the source for reprinting.

Guess you like