Home > Web Front-end > JS Tutorial > body text

'Precompilation phase' and 'execution phase' during JavaScript running process_javascript skills

WBOY
Release: 2016-05-16 15:25:26
Original
2070 people have browsed it

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 blocks<script></script>, 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> 
Copy after login

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> 
Copy after login

Execution phase:

<script>
    a = "1";
    c = function(){
      alert();
    }
</script> 
Copy after login

Overall execution steps:

<script>
    var a = undefined;
    var c = undefined;
    
    var b = function(){
      alert();
    }
    a = "1";
    c = function(){
      alert();
    }
</script> 
Copy after login

Title:

<script>
    var a = "1";
    function b(){
      alert(a);
      var a = "2";
    }
    b();
</script> 
Copy after login

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 
Copy after login

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 
Copy after login

The variable f is predefined first, and then the function f() with the same name overwrites the variable f.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template