How do we know how the JavaScript engine works

Source: Internet
Author: User

1. What is the JavaScript parsing engine?

In short, the JavaScript parsing engine is a program that can "read" JavaScript code and accurately give the result of the code running. Let's say that when you write var a = 1 + 1 , so a piece of code that the JavaScript engine does is to read (parse) Your code and change the value of a to 2.

People who have learned the principles of compiling know that for static languages (such as Java, C + +, c), called compilers (Compiler)that handle these things, the corresponding dynamic language for JavaScript is called Interpreter (interpreter). The difference between the two is summed up in a nutshell: The compiler compiles the source code into another code (such as machine code, or bytecode), and the interpreter directly parses and outputs the result of the code . For example, Firebug's console is a JavaScript interpreter.

However, it is difficult to define now that the JavaScript engine is an interpreter or a compiler, because, for example, like V8 (Chrome JS engine), it in order to improve the performance of JS, before running will be JS compiled into a local machine code (native machines Code), and then to execute the machine code (this speed is much faster), I believe that the JIT (Just in time compilation) must be familiar with it.

I personally think that there is no need to overemphasize what the JavaScript parsing engine really is, and I personally think it is possible to understand what it does. For the compiler or interpreter is how to understand the code, the university to turn out the compilation of teaching materials on it.

It is also emphasized that the JavaScript engine itself is a program, and the code is written. For example, V8 is written in C + +.

2. What is the relationship between the JavaScript parsing engine and ECMAScript?

JavaScript engine is a program, we write JavaScript code is also a program, how to let the program read the program? This requires defining the rules. For example, the previously mentioned var a = 1 + 1;it says:

    • The left Var stands for this statement (Declaration), which declares a variable
    • The + on the right indicates the addition of 1 and 1.
    • An equal sign in the middle indicates that this is an assignment statement
    • The final semicolon indicates that the statement is over.

These are the rules, and with that it's a measure of what the JavaScript engine can do to parse JavaScript code based on that standard. So the ECMAScript here is to define these rules. The document, ECMAScript 262, defines a complete set of standards for the language of JavaScript. These include:

    • Var,if,else,break,continue is the key word for JavaScript.
    • Abstract,int,long and so on are javascript reserved words
    • What kind of numbers, what kind of string, etc.
    • Operators (+,-,>,<, etc.) defined
    • Defines the syntax for JavaScript
    • Defines the processing algorithms for criteria such as expressions, statements, and so on, such as how to deal with = =
    • ??

The standard JavaScript engine will be based on this set of documents to achieve, note that the standard is emphasized here, because there are not according to standards, such as the JS engine of IE. That's why JavaScript has compatibility issues. As for why IE JS Engine does not follow the standard to achieve, it is necessary to talk about the browser war, here do not repeat, Google itself.

So, to put it simply, ECMAScript defines the language standard, which the JavaScript engine implements, which is the relationship between the two.

3. What is the relationship between the JavaScript parsing engine and the browser?

Simply put, the JavaScript engine is one of the components of the browser. Because the browser has to do a lot of other things, such as parsing pages, rendering pages, cookie management, history and so on. So, since it is part of the case, JavaScript engines are generally developed by the browser developers themselves. For example: IE9 Chakra, Firefox tracemonkey, Chrome V8 and so on.

It also shows that different browsers use different JavaScript engines. Therefore, we can only say that we want to learn more about which JavaScript engine .

4. What are the ways to gain an insight into its internal principles?

Figuring out the first three questions, that's a good answer. Personally, the main ways are as follows:

      • Read the book about how JavaScript engines work
        This is the most convenient way, but I personally understand that such a book is almost no, but Dmitry A.soshnikov blog article really is very praise, suggest directly to see English, really English looks labored, can read the translation
      • See ECMAScript's standard documentation
        This approach is relatively straightforward and original, because the engine is implemented according to standards. At present, you can see the fifth and third editions, but it is not easy to understand.
      • See JS Engine source code
        This is the most direct and, of course, the hardest. Because it also involves the implementation of lexical analyzers, parsers, and so on more underlying things, and not all engine code is open source.

How do we know how the JavaScript engine works

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.