You do not know the JS series (1) - compiler theory

JavaScript is a compiled language, it is not compiled in advance, which is the traditional compiled languages ​​different places. But it is very similar to the compile step and traditional compiled languages

 

Compile: section of the program source code before execution will go through three steps, collectively referred to as "compile"
 
1, word / lexical analysis
  The string into a code block (lexical units)
  var a = 2; decomposed into var, a, =, 2 ,; (whether space is treated as lexical unit, depending on whether the space significance)
  Lexical analysis: When the lexical unit generator is determined in an independent part of a lexical unit or other lexical units, called a stateful parsing rules, this process is called a lexical analyzer

 

2, parsing / syntax analysis
  The flow lexical unit (array), is converted into "abstract syntax tree" (AST) process
 
3, code generation
  The AST is converted to executable code of the code generation process is called
  For example, var a = AST 2 to convert a set of machine instructions to create a variable called (including memory allocation, etc.), and a value stored in a the

 

Compared to the compilation process only those three steps above and other compilers. JavaScript engine, and other complex, for example, there is a specific step in parsing and code generation for performance optimization, including optimization of redundant elements

 

However, JavaScript engine does not have a lot of time to be optimized, such as the compilation process because it does not take place before construction. Most cases occur in the compilation of a few microseconds before the code is executed, so the JavaScript compiler will first compile the program, and then execute it well prepared, it usually will be executed immediately



Guess you like

Origin www.cnblogs.com/wzndkj/p/12289876.html