What is pre-analysis?concept: Before the JS code is executed from top to bottom, the browser will parse all variable declarations first. This stage is called pre-parsing. Detailed explanation Look for var and function declarations in the scope (anonymous functions do not have function declarations, so they will not be hoisted), then declare them in advance, leave the assignment operations in place, and then execute the code from top to bottom. This is a pre-analysis process. The difference between variable and function preparsing During pre-parsing, all variables declared with Variables declared with Functions declared by Note: Anonymous functions do not have function declarations, so they will not be promoted. Repeated declaration of var variable When var is declared repeatedly: if it already exists, the compiler will ignore If it does not exist, search up along the application domain chain. If not found, the variable will be declared in this scope. Variable and function promotion precedenceSummarize: Function promotion has a higher priority than variable promotion and will not be overwritten when a variable with the same name is declared, but will be overwritten after the variable is assigned a value. The following content is reproduced from: https://blog.csdn.net/caoyafeicyf/article/details/53172532 A Deeper Look at Why Function Priority is Greater Than Variable PriorityThe browser's pre-parsing process Let’s start with a small question. var foo; function foo(){} console.log(foo); The result is the function body The result is also the function body
So many people say that the priority of function declaration is greater than that of variable declaration. So, why? This starts with the browser's pre-parsing. Pre-analysis process Search for pre-parsed keywordsLook for the var keyword Find the function keyword Perform pre-parseFirst, use the var keyword to declare the identifiers, so that these identifiers are defined. After the identifiers are defined, no error will be reported when using this identifier. However, since no value is assigned, its value is undefined. So far, the identifier holds a reference to the function. A few details to note
After reading the pre-parsing principle, let's go back to the two questions at the beginning of this article, analyze the pre-parsing process, and understand in detail why the priority of the function is higher than the priority of the variable. follow me Let’s look at the first one first var foo; function foo(){} console.log(foo); The pre-analysis process is: var foo;<----var in variable declaration var foo;<----var extracted from the function declaration foo = function () {} <----Function declaration extracted assignment console.log(foo); Let’s look at the second one function foo(){} var foo; console.log(foo); The pre-analysis process is: var foo;<----var in variable declaration var foo;<----var extracted from the function declaration foo = function () {} <----Function declaration extracted assignment console.log(foo) Compare the two, what do you notice? It turns out that their pre-parsing process is the same, which is why functions have higher priority than variables. If you understand the above content, then here is another question: var a=1; function a(){} console.log(a); How does the browser parse it? Let's follow my train of thought: 1. The parser first searches for the var keyword and finds it in the first line, extracting it to the beginning. Here is the code process processed by the parser: var a;<----var in variable declaration var a;<----var extracted from the function declaration a=function (){}<----Function declaration extracts the assignment a=1; console.log(a); The above is the detailed content of the difference between JS pre-parsing and variable promotion in web interviews. For more information about JS pre-parsing and variable promotion, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to install MySQL 5.7.29 with one click using shell script
>>: Nginx domain forwarding usage scenario code example
HTML 4 HTML (not XHTML), MIME type is text/html, ...
The road ahead is always so difficult and full of...
Use Code Cloud to build a Git code storage wareho...
This article shares the second article of using j...
When the software package does not exist, it may ...
Table of contents Preface What to use if not jQue...
1.core file When a Segmentation fault (core dumpe...
JDK download address: http://www.oracle.com/techn...
This article records the installation and configu...
1. Introduction By enabling the slow query log, M...
Uninstall MariaDB CentOS7 installs MariaDB instea...
Phenomenon When using Apache Spark 2.x, you may e...
@vue+echarts realizes the flow effect of China ma...
The browser is probably the most familiar tool fo...
Recently, in order to realize the course design, ...