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
After solving the form auto-fill problem discussed...
1. Version Information # cat /etc/system-release ...
Problem Description In the recent background serv...
Loading rules of require method Prioritize loadin...
MySQL is easy to install, fast and has rich funct...
Preface As we all know, the browser's homolog...
Excel is the most commonly used tool for data ana...
There is a new build tool in the Vue ecosystem ca...
HTTP Status Codes The status code is composed of ...
Table of contents Preface Array.isArray construct...
Preface The similarities and differences between ...
Optimizing large amounts of database data is a hu...
Preface: Vue3 has been released for a long time. ...
Table of contents 1. charAt grammar parameter ind...
Preface The project requires a circular menu. I s...