In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
It is believed that many inexperienced people have no idea about how to upgrade variables in JavaScript. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Variable promotion is like the JavaScript engine using a small code crane to lift all var and function function declarations to the top of their scope (the so-called scope, which refers to the area where variables and functions can be accessed). If you define a function outside of a function or declare a variable using var. Then the scope of variables and functions will be promoted to the highest point of the entire code, and no error will be reported when accessing the variable and calling the function anywhere; while if you define a function in the function body or use var to declare the variable, the scope of the variable and function will be raised to the highest point of the whole function, and no error will be reported when accessing the variable and calling the defined function anywhere in the function body.
Examples are as follows:
Console.log ("gv1=" + gv); / / access the variable show () before declaration; var gv = "javascript"; console.log ("gv2=" + gv); function () {console.log ("lv1=" + lv); vat lv = "js"; console.log ("lv2=" + lv);}
Output result:
Gv1 = undefined; lv1 = undefined; lv2 = js; gv2 = javasript
In the above code, the first line of Dumas and the first line of the show function accesses the gv and lv variables before the variable declaration, and the second line of code calls the show function before the function is defined. Judging from the output, the above code has no problem accessing the variable before declaration and calling the function before definition, because the variable is elevated.
The logic of the above code after pre-parsing before the code runs is as follows:
Var gv; / / variable declaration is promoted to the top of the current scope var show = function show () {var lv; console.log ("lv1=" + lv); / / lv is not initialized at the time of declaration, so the output undefined lv= "js"; console.log ("lv2=" + lv); / / the value assigned by the variable output: js} console.log ("gv1=" + gv1) / / gv is not initialized at the time of declaration, so the output undefined gv = "javascript"; console.log ("gv2=" + gv); / / the value assigned by the variable output: javascript
As you can see from above, because var supports variable promotion, variables declared by var can be used before declaration, while let and const do not support variable promotion, so the variables they declare must be declared before they can be used.
Generally speaking, the execution of javascript code includes two processes: the pre-parsing process and the line-by-line interpretation process. Before the code is read line by line, the javascript engine needs to do pre-parsing with it. During the pre-parsing process, the var variable declaration and function definition in the current scope are promoted to the highest level of the scope.
After reading the above, have you mastered the method of variable promotion in JavaScript? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.