In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how JavaScript works". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how JavaScript works.
What is JavaScript?
Let's confirm the definition of JavaScript: JavaScript is an interpreted dynamic language.
The interpretive language exists relative to the compiled language, the source code is not directly compiled into the object code, but into the intermediate code, and then the interpreter interprets and runs the intermediate code.
The mainstream programming languages are compiled (such as C++), interpreted (such as JavaScript), and semi-interpreted and semi-compiled (such as Java).
How does the code work?
First let's take a look at how the code works.
We know that the code is executed by CPU, while the current CPU cannot directly execute things such as if. Statements such as else, which can only execute binary instructions. But binary instructions are so unfriendly to humans: it is difficult for us to quickly and accurately determine what a binary instruction 1000010010101001 represents. So scientists invented assembly language.
Assembly language
Assembly language is actually a mnemonic for binary instructions.
Assuming that 10101010 represents a read memory operation, the memory address is 10101111 and the register address is 11111010, then the complete operation 1010101010111111111010 represents reading the value of a memory address and loading it into the register, and the assembly language does not change this mode of operation, it is just a mapping of binary instructions:
LD:10101010 id:10101111R:11111010
In this way, the above instructions can be expressed as LD id R, which greatly enhances the readability of the code.
But this is not friendly enough. CPU can only execute three-address expressions, which is a far cry from people's way of thinking and language patterns. So the great scientists invented the advanced language.
high-level language
"the code is written for people, not for machines, but by the way the computer can execute it."
High-level language is called "advanced" because it is more in line with our thinking and reading habits. If... The sentence else looks much more comfortable than 1010101010. But the computer cannot directly execute the high-level language, so it still needs to convert the high-level language into assembly language / machine instructions to execute. This process is compilation.
Does JavaScript need to be compiled?
JavaScript is undoubtedly a high-level language, so it must need to be compiled before it can be executed. But why do we call it an interpretive language? How is it different from compiled language and semi-interpreted semi-compiled language? Let's start with compilation.
Compile
Now that we know the concept of compilation, let's talk about the platform: the same C++ code is compiled into an .obj file on Windows, while a .o file is generated on Linux. This is because an executable file needs operating system API, memory, threads, processes and other system resources in addition to code, and different operating systems have different implementations. For example, we are familiar with Imax O Multiplexing (event-driven Soul), which is implemented in IOCP on Windows and epoll on Linux. Therefore, for different platforms, compiled languages need to be compiled or even written separately, and the format of the generated executable files is different.
Cross platform
Java goes a step further by introducing bytecode to run across platforms: .java files are compiled into .class files on any operating system (this is the bytecode file, an intermediate form of object code). Java then provides different Java virtual machines for different systems to interpret and execute bytecode files. Interpretive execution does not generate object code, but it is eventually converted to assembly / binary instructions for computer execution.
If we write a simple operating system completely independently, can it run Java? Obviously not, because there is no corresponding JVM for this system. So the cross-platform of Java, the cross-platform of any other language, is limited.
The advantage of Java using semi-interpretation and semi-compilation is that it greatly improves the development efficiency, but correspondingly reduces the execution efficiency of the code. after all, the virtual machine has performance loss.
Interpretive execution
JavaScript goes a step further. It is fully interpreted execution, or just-in-time compilation. There will be no intermediate code generation, and there will be no object code generation. This process is usually arranged by the hosting environment (such as browser, Node.js).
Compilation process
Now we have confirmed that even the language of interpretation execution needs to be compiled. So how does the code compile? Let's have a brief understanding.
Lexical analysis
Lexical analysis breaks down statements into lexical units, namely Token.
Function square (n) {return n;}
This function is recognized by the lexical analyzer as function, square, (, n,), {, return,n, *, n,} and tagged to indicate whether it is a variable or an operation.
Grammar analysis
This process converts Token into an abstract grammar tree (AST):
{type:'function', id: {type:'id' name:'square'}, params: [{type:'id', name:'n'}]...}
Optimization and code generation
In this step, the compiler will do some optimization work, such as deleting redundant operations, deleting unused assignments, merging some variables, and finally generating object code.
Because the compilation of just-in-time compiled languages usually occurs a few microseconds before running, the compiler does not have time to do much optimization work. This is also one of the reasons why the performance of early JavaScript is weak compared to compiled languages. For now, however, the performance gap between JavaScript and other languages is not enough thanks to the V8 engine (compared to the earlier JavaScript engine conversion to bytecode or interpretation execution, Node.js can use the JS2C tool provided by V8 to translate JavaScript into C++ code).
Linking and loading
The target code basically cannot be run independently. Applications are generally composed of multiple parts (modules). For example, a simple output in C++ requires the introduction of the standard library iostream:
# include using namespace std;int main () {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.