In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares to you is about what is the difference between JS pre-parsing and variable promotion in web interviews. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
What is pre-resolution?
Concept:
Before the JS code executes from top to bottom, the browser parses all variable declarations, which is called pre-parsing.
Tell it in detail
Look for var and function declarations in scope (anonymous functions do not have function declarations, so they are not promoted), then declare them in advance, leave the assignment in place, and execute the code from top to bottom. This is a pre-parsing process.
The difference between variable and function pre-parsing
When pre-parsing, all variables declared in var and functions declared by function are promoted to the top of their scope
The variable declared by var is only declared in advance when it is pre-parsed, and the assignment statement remains in place.
The function declared by function will be declared and defined in advance when it is pre-parsed, and it will only be pre-parsed inside the function when the function is executed.
Note: anonymous functions have no function declaration, so they will not be promoted
Repeatedly declare var variables
When var repeats the declaration: if it already exists, the compiler will ignore var and continue to compile downwards
If it does not exist, look up along the scope chain
If it is not found, the variable is declared in this scope
Variable promotion and function promotion priority
Summary:
The priority of function promotion is higher than that of variable promotion, and it will not be overridden when the variable of the same name is declared, but it will be overridden after the variable is assigned.
The following is reproduced from:
Https://blog.csdn.net/caoyafeicyf/article/details/53172532
An in-depth study of the priority of function is greater than the priority of variables
Pre-parsing process of browser
Let's start with a small question and enter this article.
Var foo;function foo () {} console.log (foo)
The result is the function body function foo () {}
Then the following question:
Function foo () {} var foo;console.log (foo)
The result is also the body of the function
Function foo () {}
Many people say that the priority of a function declaration is higher than that of a variable declaration.
So, why? This starts with the pre-parsing of the browser.
Pre-parsing process searching for pre-parsed keywords
Looking for the var keyword
Looking for the function keyword
Perform pre-parsing
First apply the identifiers declared by the var keyword to make these identifiers defined
Once the identifier is set, there will be no error using this identifier, but because there is no assignment, the value is undefined
At this point, a reference to the function is saved in the identifier
A few details to pay attention to
When the var keyword is repeatedly used for the same identifier, it is ignored except for the first time it is valid.
When pre-parsing, first deal with the variable declaration, and then process the function declaration no
It is only superficial to struggle with who has the highest priority.
Knowing the pre-resolution process, everything is a cloud.
After looking at the principle of pre-parsing, let's go back to the two questions at the beginning of this article, analyze the process of pre-parsing, and understand in detail why functions take precedence over variables. Follow me
Look at the first one first.
Var foo;function foo () {} console.log (foo)
The pre-resolution process is as follows:
Var foo
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.