In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "improving the working principle in JavaScript", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the working principle of ascending in JavaScript" article.
Hoisting is the concept of JavaScript, which makes JavaScript different from the Java language. In Java, every variable created in the code has a block-level scope. This means that if we create any variable that limits its visibility to the block in which it is declared. Therefore, if we use the above variables in the declaration, we will make an error. In JavaScript, however, variables can be used before they are declared, a mechanism called Hoisted. This is the default behavior of JavaScript.
Promotion is the default behavior of JS, which defines all declarations at the top of the scope before the code is executed. One of the benefits of promotion is that it enables us to call functions before they appear in the code. JavaScript only promotes the declaration, not initializes it.
Understand what the JavaScript promotion variable is declared and initialized in the following order:
Declaration-> initialization / assignment-> usage
/ / Variable lifecyclelet x; / / Declarationx = "hoisting"; / / Assignmentconsole.log (x); / / Usage
Most importantly, you should always remember that JavaScript declares variables in the background first. Then, initialize them. Therefore, it is also good to understand that the processing of variable declarations occurs before any code is executed. However, there are no undeclared variables in JavaScript until the code that assigns them is executed. Therefore, when you perform an assignment, the value assigned to an undeclared variable implicitly creates it as a global variable. This specifies that all undeclared variables are global.
/ / hoistingfunction Hoisting () {x = 100; let y = 200;} Hoisting (); console.log (x); / / 100console.log (y); / / Reference Error: y is not defined
In the above code example, there is a function named Hoisting (). Therefore, we have a variable that is not declared with let/var/const and a let variable y. Assigning undeclared variables to a global scope is done by JavaScript. But for the variable y, we get a reference error (Refence Error).
Managed in function-scoped variables
In ES5, we consider the var keyword. Compared to let/const, using var for promotion is a little different. Use var to see an example of how ascension works:
Var num (global) console.log (car); / / undefinedvar car = 'Lamborgini'
In the above code, when recording the variable names declared and assigned after using it, the compiler gives the result of "undefined". This is to be expected, because we should try to use the car variable before declaring it, because we should get ReferenceError. But the interpreter has a different view on this, as follows:
/ / how interpreter sees the above codevar car;console.log (car); / / undefinedcar = 'Lamborgini';let and const keywords.
Variables and constants declared with let or const will not be promoted!
JavaScript initialization is not initialization.
JavaScript can only promote the declaration, not initialize it.
Var a = "volkswagon"; / / Initialize avar b = "Lamborgini"; / / Initialize belem = document.getElementById ("car"); / / Find an Elementeleme [XSS _ clean] = a + "" + b; / / Display an and b as volkswagon and lamborgini
In the above code, because the declaration of the variable occurs before the result. As a result, the execution of the code prints the results of the variables an and b.
Var a = "i10"; / / Initialize aelem = document.getElementById ("car"); / / Find an Elementeleme [XSS _ clean] = "an is" + a + "and b is" + b; / / Display an and bvar b = "Lamborgini"; / / Initialize b
Results:
An is i10 and b is undefined.
Therefore, this is because only the promotion of the declaration (var b) occurs, rather than initializing (= "Lamborgini") to the top. Due to promotion, b is declared before use, but because initialization does not promote, the value of b is undefined.
Promotion class (Hoisting Classes)
The JavaScript class can be divided into two categories:
Class declaration
Class expression
In class declarations, they are much like function equivalents. Therefore, this means that the JavaScript class declaration will not be promoted. However, they remain uninitialized until evaluated. Therefore, this actually means that a class must be declared before it can be used.
Var car1 = new car (); car1.height = 5policar1.weight = 500 console.log (car1); / / Output: ReferenceError: car is not definedclass car {constructor (height, weight) {this.height = height; this.weight = weight;}}
In the above code, a reference error occurred. This is because the definition of the car class occurs after the car1 variable is initialized. To resolve this error, we just need to define the car class before car1 initialization. This is managed in the class declaration.
Class car {constructor (height, weight) {this.height = height; this.weight = weight;}} var car1 = new car (); car1.height = 5 countries car1.weight = 500 console.log (car1)
Therefore, this gives the correct results. They are very similar to their functional counterparts in Class expressions. Therefore, this means that there is no elevated class expression. Therefore, here is an example of an unnamed or anonymous variant of a class expression:
Var rect = new shapes (); rect.height = 10 height console.log (rect); / / Output: TypeError: shapes is not a constructorvar shapes = class {constructor (height, width) {this.height = height; this.width = width;}}; Thus, the correct way to do it is like this:var shapes = class {constructor (height, width) {this.height = height; this.width = width;}}; var rect = new shapes (); rect.height = 10 Rect.width = 20position console.log (rect); the above is the content of this article on "how to improve how it works in JavaScript". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.
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: 222
*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.