"var a=1;" What's going on in JS?
A very basic question, but to answer it, you need to know something about JavaScript engines.
Introduction to JavaScript Engines
What is a JavaScript engine? Simply put, it is a runtime environment that can process and execute JavaScript code. In general terms, when you write var a = 1 + 1; With such a piece of code, all the JavaScript engine does is read (parse) your piece of code and change the value of A to 2. However, to really explain JavaScript engines, we need to understand some of the basic concepts of compilers and some of the new compilation techniques that modern languages require.
What language does JavaScript belong to? Compiled or interpreted?
What is the difference between compiled and interpreted languages? To answer this question, take a crude analogy: in a restaurant, everything is served to you in one sitting, already prepared in the kitchen. And the explanation type is the customer when repast, the kitchen now fry now do.
Let’s start with a typical compiled language: C/C++. In order for the machine to understand what you are writing, the language usually uses a compiler to compile the source code into native code. This process is called compilation and is compiled before the code is executed. The operating system schedules the CPU to execute the compiled native code directly, with no additional operations. The complete compilation process that takes place before the code is executed is called AOT(Ahead of Time).
Typical interpreted languages are scripting languages such as Python. The usual way to deal with scripting languages is for the developer to send the written code directly to the user, who uses the script’s interpreter to load the script and interpret it for execution. Typically, scripting languages do not require developers to compile script code.
Now look at the Java language. It belongs to a variety and can be understood as a hybrid type. To eliminate the disadvantage of C/C++ being unfriendly to different operating systems, Java introduced the slogan “Write once, Run everywhere” to implement the compilation process independent of the specific operating system. Its approach is divided into two stages:
- The first is the compilation phase, as in C++. But instead of native code, the compiler generates bytecode. Bytecode is a cross-platform intermediate representation that is platform-independent and can run on different operating systems.
- Run bytecode phase. Java runs in an environment where the Java VIRTUAL machine loads bytecodes and uses an interpreter to execute the bytecodes. But that alone is certainly slower than C++. Modern Java virtual machines use just-in-time (JIT) technology to improve efficiency: just-in-time compilation. JIT technology improves efficiency by converting bytecode into native code.
Finally, back to the JavaScript language. When the language was designed, its main purpose was to handle input validation operations that had previously been handled by the server, and it was a completely interpretive scripting language that only needed to be interpreted using an interpreter. But with the rise of the Internet and browsers, it can do more and more powerful. Engineers have devoted resources to making it faster, improving the way it works by borrowing from Java virtual machines and C++ compilers. One of the key technologies is JIT technology in the Java Virtual Machine, which converts abstract syntax trees into intermediate representations and then into native code with JIT technology, which can greatly improve execution efficiency.
At first glance, JavaScript is similar to Java compilation. But in reality, the differences between the two are as follows:
- Type. JavaScript is an untyped language (also known as a dynamic language), and its access to object representations and properties suffers from a greater performance penalty than Java.
- Point in time for the JIT phase. The Java language typically compiles source code to bytecode and then executes it at execution time, meaning that compilation and execution are separated, meaning that the amount of time between source code, abstract tree, and bytecode doesn’t matter; you just need to generate bytecode as efficiently as possible. But for JavaScript, the JIT process is implemented in conjunction with the web page load execution phase after the JavaScript source file is downloaded, so the time handling of this process can be very demanding.
But in reality, the JavaScript engine’s execution steps are quite complex. With JavaScript, most of the compilation takes place a few subtleties (or less!) before the code executes. . So as little time in each phase as possible. Different JS engines have their own unique optimization work in different steps. I won’t expand it here.
So, from this perspective, we can try to answer the question of what language JavaScript belongs to. When the language was designed, it was really an interpretive language. It can be considered a compiled language at a time when engineers are trying to introduce more compiled language-related technologies (JIT, bytecode, etc.).
What does the JavaScript engine actually do?
So back to our problem: “Var a=1; What’s going on in JS?
You might think that’s a statement. But our engines don’t see it that way. In fact, the engine thinks there are two completely different declarations, one handled by the compiler at compile time and the other handled by the engine at run time.
As you can see, when we look at the JavaScript engine at this point, we can divide it into two phases: compilation phase and execution phase.
We specifically created two characters: the engine and the compiler to answer the article title questions in detail. As follows:
When var a is encountered, the compiler asks the scope if there is already a variable of that name that exists in the same scope. If so, the compiler ignores the declaration and continues compiling; Otherwise he will ask the scope to declare a new variable in the collection of the current scope and name it a. The compiler then generates run-time code for the engine to handle the assignment of a=1. The engine starts by asking the scope if there is a variable called A in the current scope. If so, the engine uses this variable; If not, the engine continues to look for the variable (through the scope chain). If the engine eventually finds the A variable, it assigns 1 to it. Otherwise the engine will throw an exception!
JavaScript in-depth series:
“var a=1;” What’s going on in JS?
Why does 24. ToString report an error?
Here’s everything you need to know about “JavaScript scope.
There are a lot of things you don’t know about “this” in JS
JavaScript is an object-oriented language. Who is for it and who is against it?
Deep and shallow copy in JavaScript
JavaScript and the Event Loop
From Iterator to Async/Await
Explore the detailed implementation of JavaScript Promises