Home >Web Front-end >JS Tutorial >'Precompilation phase' and 'execution phase' during JavaScript running process_javascript skills
Compared with other languages, javascript is a weakly typed language. In other languages such as java, the execution of the program requires a compilation stage, and there is a similar "pre-compilation stage" in javascript (javascript's pre-compilation stage). Compilation is based on code blocks3f1c4e4b6b16bbbd69b2ee476dc4f83a2cacc6d41bbb37262a98f745aa00fbf0, that is, every time a code block is encountered, it will be pre-compiled>executed). Understanding the execution mechanism of the javascript engine will be helpful in the process of writing js code. Summary of ideas
First of all, let’s learn about the two declaration methods in JavaScript, var and function. The former declares variables, and the latter declares methods
In pre-compilation, javascript makes two processing solutions for these two declarations
<script> var a = "1"; //声明变量a function b(){ //声明方法b alert(); } var c = function(){ //声明变量c alert(); } </script>
In the above code block, a and c are variable assignments, and b is a function declaration. When the above code is executed, it will first enter the pre-compilation stage. Assigning a and c to variables will open up a memory space in the memory and Points to the variable name, and the assigned value is undefined
For function declaration, the memory space will also be allocated, but the assigned object will assign the declared function to the function name
Pre-compilation phase: (PS: Regardless of the order in which variables and functions are declared in the code, variables will be declared first and then functions in the pre-compilation phase)
<script> var a = undefined; var c = undefined; var b = function(){ alert(); } </script>
Execution phase:
<script> a = "1"; c = function(){ alert(); } </script>
Overall execution steps:
<script> var a = undefined; var c = undefined; var b = function(){ alert(); } a = "1"; c = function(){ alert(); } </script>
Title:
<script> var a = "1"; function b(){ alert(a); var a = "2"; } b(); </script>
ps: pre-compilation of javascript
1. Predefine variables first, then predefine functions
2. Pre-compilation of variables only declares them, does not initialize them, and initializes them during execution
3. The function defined by the function statement not only declares the function name, but also processes the function body
4. Anonymous functions will not be precompiled
function f(){ // 声明函数f return 1; } alert(f()); // 返回1 var f = function(){ // 定义匿名函数f return 2; } alert(f()); // 返回2
The variable f is predefined first, and then the function f() with the same name overwrites the variable f, so 1 is output for the first time; Precompilation of variables
var f = function(){ // 定义匿名函数f return 1; } alert(f()); // 返回1 function f(){ // 声明函数f return 2; } alert(f()); // 返回1
The variable f is predefined first, and then the function f() with the same name overwrites the variable f.