Today, I spent a lot of time reviewing scopes, precompiling, and so on.
Read a lot of posts, opened the previous read the book
found that they have learned very clearly, in fact, there are some misconceptions of thinking
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 in detail
You have to understand that this precompilation and compilation is not the same
JavaScript is an interpreted language,
Since it is an interpreted language, it compiles a line, executes a line
Traditional compilation will go through many steps, such as participle, parsing, code generation, etc.
We will have time to give you more popular science later
The following is to share what I understand JS pre-compilation
JavaScript Run Trilogy
What did you do when the page was loaded?
- Syntax analysis
- Pre-compilation
- Interpreting execution
There are two more steps before executing the code
Parsing is simple, that is, the engine checks your code for any low-level syntax errors.
Interpreting execution as the name implies is code execution.
Pre-compilation simple understanding is to open up some space in memory, storing some variables and functions
Understanding that precompilation is also 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 running trilogy above make you misunderstand,
Mistakenly assume that pre-compilation occurs before the code block in script executes
There's nothing wrong with that.
Precompilation does happen before execution in script code.
But most of it happens before the function is executed .
JS Pre-compilation instance
For example, consider these concepts first:
- Variable declaration var ...
- function declaration functions ...
<script> var a = 1; //Variable declaration function b(y){//Functions declaration var x = 1; Console.log ('so Easy '); }; var c = function (){//is a variable declaration instead of a functional declaration!! //... } b (+); </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 is loaded
- Parsing syntax is legal when the script is loaded
- Start precompilation
- Lookup variable declaration, as Go property, value assigned to undefined
- Find function declaration, as Go property, value assigned to function body
//伪代码GO/window = { //页面加载创建GO同时,创建了document、navigator、screen等等属性,此处省略 undefined, undefined, function(y){ var1; console.log(‘so easy‘); }}
- Interpreting execution code (until function B is executed)
//伪代码GO/window = { //变量随着执行流得到初始化 1, function(){ //... }, function(y){ 1; console.log(‘so easy‘); }}
- Pre-compilation occurs before function B is executed
- Create an AO activity object (Active objects)
- Find formal parameters and variable declarations, with values assigned to undefined
- Argument values are assigned to parameters
- Find function declaration, value assigned to function body
//伪代码AO = { //创建AO同时,创建了arguments等等属性,此处省略 100, undefined}
- Interpreting code in an execution function
- The first script file finishes executing, loading the second script file
- After the second script file is loaded, parse
- Parsing complete, start precompilation
- Repeat the first precompilation step ...
We must note that
variable declaration and function declaration occur during precompilation, no initialization behavior (Assignment), anonymous function does not participate in precompilation
variable initialization is only performed during the interpretation of the execution phase
Well, finally, take the tail.
Summarize
Pre-compilation (before function execution) ※
1. Create an AO object (Active object)
2. Find function parameters and function inside variables declaration, parameter name and variable name as properties of AO object, value is undefined
3. The argument is uniform, and the parameter value is assigned to the formal parameter.
4. Find function declarations, function names as properties of AO objects, values as function references
Pre-compilation (script code block before execution)
1. Find global variable declaration (including implicit global variable declaration, omit var declaration), Variable masterpiece Global object property, value undefined
3. Find function declarations, function names as properties of global objects, values as function references
Understand the pre-compilation is very helpful to understand the behavior of ascension, this point, scope and performance.
I'm going to summarize these questions later. +_+, I'm tired.
JavaScript Pre-compilation