简体   繁体   English

在Web浏览器环境中执行时,JavaScript是否编译为机器代码?

[英]Is JavaScript compiled to machine code when executed in a Web Browser Environment?

I'm trying to get a better understanding of how JavaScript is executed in a web browser environment. 我正在努力更好地了解JavaScript在Web浏览器环境中的执行方式。

In terms of Node.js, I understand that the JavaScript code written in a Node.JS program is compiled with C++ code (V8), and ultimately becomes machine code. 就Node.js而言,我理解在Node.JS程序中编写的JavaScript代码是用C ++代码(V8)编译的,最终成为机器代码。 Since Node.js can interact with the filesystem and other machine level tasks, to me it makes sense why it has to eventually become machine code. 由于Node.js可以与文件系统和其他机器级任务进行交互,因此对我来说最终必须成为机器代码才有意义。

I feel differently about the web browser environment. 我对Web浏览器环境有不同的看法。 From my understanding, the main goal is to interact with the DOM. 根据我的理解,主要目标是与DOM进行交互。 Does JavaScript need to be compiled into machine code to just interact with the DOM? JavaScript是否需要编译成机器代码才能与DOM交互?

I'm puzzled by this. 我很困惑。 Node.js and Chrome both run on V8. Node.js和Chrome都在V8上运行。 V8 is written in C++ and to my knowledge, compiles JavaScript code into machine code a processor can understand. V8是用C ++编写的,据我所知,它将JavaScript代码编译成处理器可以理解的机器代码。

You need a JavaScript engine to implement ECMA-262, that is the whole purpose of an engine (I think?). 你需要一个JavaScript引擎来实现ECMA-262,这就是引擎的全部目的(我想?)。 But, does a web browser need JavaScript to be compiled to a Machine Language Level, what machine operations is it performing? 但是,Web浏览器是否需要将JavaScript编译为机器语言级别,它正在执行哪些机器操作?

Here are a few articles I've researched, unfortunately, I haven't found an answer to my question in them: 以下是我研究的一些文章,不幸的是,我还没有找到我的问题的答案:

The engine is written in C++, then this code is translated into machine code by a compiler. 引擎是用C ++编写的,然后这个代码由编译器转换成机器代码。 Once the code is in machine language, it can be run by the computer. 一旦代码是机器语言,它就可以由计算机运行。 While the engine is running, it can read code written in JavaScript, interpret it, and execute what the code is asking it to do. 当引擎运行时,它可以读取用JavaScript编写的代码,解释它,并执行代码要求它执行的操作。 In this case, what is actually running in the computer is the engine code, that just happens to be doing what another code is telling it to do. 在这种情况下,计算机中实际运行的是引擎代码,恰好正在执行另一个代码告诉它要执行的操作。 The difference between node and a browser is that the browser won't do anything that a JavaScript is asking it to do. 节点和浏览器之间的区别在于浏览器不会执行JavaScript要求它执行的任何操作。 Another thing to keep in mind is that some browsers and node translates JavaScript code to machine code in real time to get more speed. 另外要记住的是,某些浏览器和节点会将JavaScript代码实时转换为机器代码以获得更快的速度。 Browsers are also careful not to write machine code that is dangerous, but in theory that could happen. 浏览器也小心不要编写危险的机器代码,但理论上可能会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM