Today, I spent a lot of time reviewing scopes, precompilation, and so on.
Looked very Doboven, turned over once read the book (as if many books did not say pre-compilation)
found that they have learned very clearly, in fact, there are some mistaken thinking
(Very Doboven misleading)
I'm going to tidy up some messy ideas tonight.
First of all, compile the knowledge of pre-compilation, and then have time to explain the scope of the specific
Let's be clear. This precompilation is not the same as the traditional compilation (can understand JS pre-compilation as a special compilation process)
JavaScript is an interpreted language,
Since it is an interpreted language, it is a compilation line. Run a row
Traditional compilations go through a lot of steps, word breakers, parsing, code generation, etc.
We will have time to give you more popular science later
The following is to share with you what I understand JS pre-compilation
JavaScript Run Trilogy
What does the JavaScript engine do when the script runs?
- Syntax analysis
- Pre-compilation
- Explanation Run
Before running the code. There are two more steps
Syntax analysis is very easy. Is that the engine checks your code for any low-level syntax errors.
The explanation runs as the name implies running the code.
Pre-compilation simple understanding is to open up some space in memory, storing some variables and functions
Understand that precompilation is helpful for everyone to understand the scope
JS pre-compilation when it happens
I thought the wrong way of thinking happened here.
When does precompilation happen
I hope you don't let the above operation process make you misunderstand,
Mistakenly assume that precompilation only occurs before the code block in script runs
There's nothing wrong with that.
Precompilation actually happens before script code runs.
But most of it happens before the function runs .
JS Pre-compilation instance
Example before. Consider these concepts first:
- Variable declaration var ...
- function declaration functions ...
<script ; var a = 1 ; //variable declaration function b (y) { //function declaration var x = 1 ; Console.log ( ' so easy ' ); }; var c = function Span class= "Hljs-params" > () { //is a variable declaration instead of a function declaration!
! //... } b(100);</script><script> var d = 0;</script>
Let's see what the engine did to this code.
- Page generation creates a Go global object (also known as a Window object)
- The first script file loads
- After script loading is complete. Parsing syntax is legal
- Start pre-compilation
- Find the variable declaration as the Go property. Value given to undefined
- Find the function declaration. As the Go property, the value is given to the function body
//伪代码GO/window = { //页面载入创建GO同一时候。创建了document、navigator、screen等等属性。此处省略 undefined, undefined。 function(y){ var1; console.log(‘so easy‘); }}
- Explanation of running code (until function B is run)
//伪代码GO/window = { //变量随着运行流得到初始化 1, function(){ //... }, function(y){ 1; console.log(‘so easy‘); }}
- Before running function B. Pre-compilation occurs
- Create an AO activity object (Active objects)
- Find shape and variable declarations, value assigned to undefined
- The actual value is assigned to the shape
- Find function declaration, value assigned to function body
//伪代码AO = { //创建AO同一时候。创建了arguments等等属性。此处省略 100, undefined}
- Interpreting code in a run function
- The first script file runs complete. Loading a second script file
- After the second script file is loaded, the parsing is done
- Parsing is complete. Start pre-compilation
- The pre-compilation step that starts again and again ...
We must note that
variable declarations and function declarations occur during the precompilation phase. No initialization behavior (Assignment), anonymous function does not participate in precompilation
variable initialization is only performed during the interpretation of the run phase
Well, finally, take the tail.
Summarize
Pre-compilation (before function) ※
1. Create an AO object (Active object)
2. Find function parameters and variable declarations within functions. The name and variable name as attributes of the AO object with a value of undefined
3. The real participation is unified, the actual value is assigned to the formal participation
4. Find function declarations, function names as properties of AO objects, values as function references
Precompilation (script code block before run)
1. Find the global variable declaration, which contains an implicit global variable declaration. omit Var declaration), the property of the variable masterpiece global object, the value is undefined
3. Find the function declaration. Function name as a property of a global object with a value of function reference
Understand that precompilation can be very helpful in understanding the behavior of ascension, this direction, scope, and performance.
I'll summarize these questions later.
= = Home Portal = =
JavaScript pre-compilation principle analysis