Simple understanding of JavaScript compilation principles and differences (scope and closure - Ch1.1)

variable

The most fundamental feature in a programming language is that the ability to变量 store and access brings to programs. The question is: where is the variable stored? More importantly how does the program find them? Explain that you need to design a good set of rules to store variables and find them easily, and that's it .变量状态

作用域

traditional compiled language

A piece of source code in a program goes through three steps, collectively 编译:

  • Lexical analysis:
    decompose each piece of code into meaningful code blocks
    such var = 1as into var, =, 1.
  • Syntax analysis
    This process 词法单元流converts into a tree that represents the grammatical structure of the program, called 抽象语法树,
    for example VariableDeclaration, a node that contains a Identifier = varand AssignmentExpressionnode,
    and AssignmentExpressionthat contains NumericLiteral = 1a node.
  • Code generation converts
    into executable code, and this process is closely related to . Simply put, there is some way to turn the of into a series of machine instructions to perform this operation.抽象语法树语言、目标平台
    var = 1抽象语法树

JavaScript engine

  • The JavaScript 语法分析engine 代码生成has specific steps in the and process to optimize the running performance.
  • JavaScript engines don't have a lot of time to optimize, and the compilation process doesn't happen before the build.
    Usually the JS code fragment is compiled before execution, and after compilation, it will be ready for execution and executed immediately.

Guess you like

Origin blog.csdn.net/Littlelumos/article/details/128648328