Wechat official account: [Dafront-end Yizhan] Follow Dafront-end Yizhan. Questions or suggestions, welcome the public account message.

This is the 21st day of my participation in the August Text Challenge.More challenges in August

Compilation principle

We often categorize JavaScript as a “dynamic” or “interpreted execution” language, but in fact it is a compiled language. Depending on how many programming languages you’ve been exposed to and how much experience you have, this fact may be obvious to you or you may not have heard of it. However, unlike traditional compiled languages, it is not compiled ahead of time and the compiled results cannot be migrated to distributed systems.

In the flow of a traditional compiled language, a piece of source code in a program goes through three steps, collectively called “compilation,” before it is executed.

Word Segmentation/Lexical Analysis (Tokenizing/Lexing)

This process breaks down a string of characters into blocks of code that make sense (to the programming language), called lexical units (tokens). For example, consider the program var a = 2; . This program is usually broken down into the following lexical units: var a = 2; . Whether Spaces are considered lexical units depends on whether they make sense in the language.

The distinction between tokenizing and Lexing is subtle and obscure, with the main difference being whether the identification of lexical units is done statically or stateless. Simply put, if a lexical unit generator invokes stateful parsing rules to determine whether A is an independent lexical unit or part of another lexical unit, the process is called lexical analysis.

Parsing/Parsing

This process converts a stream of lexical units (arrays) into a hierarchical nested tree of elements that represents the syntactic structure of the program. This Tree is called the Abstract Syntax Tree (AST). var a = 2; The abstract syntax tree may have a top-level node called VariableDeclaration, followed by a child node called Identifier (whose value is a), and a child node called AssignmentExpression. The AssignmentExpression node has a child called NumericLiteral (whose value is 2).

Code generation

The process of turning an AST into executable code is called code generation. This process depends on language, target platform, and so on.

Regardless of the details, the short answer is that there is some way to set var a = 2; The AST is converted to a set of machine instructions that create a variable called A (including allocating memory, etc.) and store a value in A.

JavaScript engines do not have as much time to optimize as compilers in other languages do, because unlike other languages, the compilation process for JavaScript does not take place prior to build. Any snippet of JavaScript code is compiled before execution (usually just before execution). Therefore, the JavaScript compiler first evaluates var a = 2; The program compiles, is ready to execute, and usually executes immediately.














Pay attention to the bottom [Big front End station]
Let’s learn and make progress together

[Share, like, watch] Let more people join us