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--
This article will explain in detail what is meant by pre-compilation and translation in javascript. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
In JavaScript, precompilation is an operation that takes place before the code is executed, in which variables and functions are declared in advance, and these are put into the created object according to certain rules.
The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.
What is precompilation in javascript
Js running Trilogy
1. Parsing: before parsing the js code, the js engine scans the whole text to find low-level syntax errors, such as miswriting braces.
two。 Any pre-compiled syntax and statements will be converted into objects. GO (Global Object), AO (Active Object) put the code into GO and AO according to certain rules.
3. Interpretation executes one line of compilation, and when there is no problem with parsing, and after the precompilation phase has been completed, the interpretation and execution of the code begins.
Pre-compilation of what?
Pre-compilation is an operation in front of the JavaScript code that will advance the variable declaration and the function declaration, and put the code in the created object according to certain rules.
The process of precompilation:
GO window precompilation
1, create a GO object as soon as the script tag is executed
2, look for the variable declaration, the variable name is the property name of the GO object, and the value is undefined.
3. Find the function declaration. The function name is the property name of the GO object, and the value is the function body. If the function name is the same as the variable name, override it directly.
Precompilation of AO function
1. Create an AO object as soon as the function executes
2, look for arguments and formal parameters, add formal parameters to the object as the property name of the AO object, and the value is the argument. It is worth noting that the function declaration is not called a variable. If there is no actual parameter value, it is undefined.
3, look for the variable declaration, the variable name is the property name of the AO object, and the value is undefined. If the variable name is the same as the formal parameter name, it doesn't matter.
4. Find the function declaration. The function name is the property name of the AO object, and the value is the function body. If the function name is the same as the variable name, override it directly.
Next, let's write a piece of code to briefly introduce the process of precompiling a ha function.
Function fun (var b) {console.log (a); var a = 10; console.log (a); function a () {} console.log (a) a = 1; var b; console.log (b); var b = function () {} console.log (b);} fun (1Jol 2)
1 first creates an AO object before executing fun.
FunAO {}
2, look for arguments and formal parameters, add formal parameters to the object as the property name of the AO object, and the value is the argument. It is worth noting that the function declaration is not called a variable. If there is no actual parameter value, it is undefined.
FunAO {a: 1, b: 2}
3, look for the variable declaration, the variable name is the property name of the AO object, and the value is undefined. If the variable name is the same as the formal parameter name, it doesn't matter.
FunAO {a: 1, b: 2}
4. Find the function declaration. The function name is the property name of the AO object, and the value is the function body. If the function name is the same as the variable name, override it directly.
FunAO {a: function a () {}, b: 2}
Interpretive execution
Function fun (console.log b) {console.log (a); / / look in the AO object above, where the value of an is function a () {}. Here is the output function a () {} var a = 10; console.log (a); / / the above line assigns a to 10, so here is 10 function a () {} console.log (a) / / because the above function has advanced a = 1; var b; console.log (b); / / look in the AO object above, and the value of b is 2. Here is the output 2 var b = function () {} / / Why the function here can not be advanced, because here is a function expression, this function does not have a function name, so it cannot be advanced. Console.log (b); / / here assign b to function () {}, so here output function () {}} fun (1Jue 2)
There is another possibility.
Function add () {/ / if you encounter if judgment here, for.... We all have to analyze normally except for the function scope, / / so the value here is undefined, which actually means that var an assigns undefined console.log (a) in advance; / / what is the output here? Undefined if (a) {var a = 10;} / here is to pass the value of an into the judgment, and undefined returns false, so if you can't get in, the value will not change here or undefined console.log (a); / / undefined} add ()
This is the execution process of precompilation!
This is the end of the article on "what does pre-translation in javascript mean?". I hope the above content can be of some help to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.