Javascript 是编译还是两遍解释?

发布于 2024-12-02 08:31:03 字数 237 浏览 0 评论 0原文

我是一名公认的 JavaScript 程序员新手,并且正在尝试了解更多信息。所以我向你们寻求帮助,提出这个简单的问题:)。我正在阅读的 O'Reilly 书中不断提到 JavaScript 代码的编译时。我对函数式编程(方案等)的了解告诉我,JavaScript 实际上是由浏览器解释的,很可能需要两次通过 JavaScript。

我的评估有误吗?或者本书引用的编译时实际上只是解释器的第一遍,类似于 Perl 或 Python 的功能?谢谢!

I'm an admitted novice JavaScript programmer and am attempting to learn more. So I turn to you folks for help, with this easy question :). The O'Reilly book that I'm reading keeps referring to the compile-time of the JavaScript code. My knowledge of functional programming (scheme and the likes) tells me that the JavaScript is actually interpreted by the browser, most likely requiring two passes through the JavaScript.

Am I incorrect in my assessment? Or is the compile-time that the book references actually just the first pass of the interpreter, similar to how Perl or Python would function? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺渁∝ 2024-12-09 08:31:03

它依赖于浏览器。查看 WebKit 的 SquirrelFish Extreme 和 Google V8 以了解最快的结果,并查看 Mozilla 的 JaegerMonkey 的实现。

AFIAK V8 和 SFX 是 JIT,因此它们将 JS 代码编译为本机。 JaegerMonkey 和 TraceMonkey 在 Firefox 中结合起来形成一个系统,如果代码跟踪速度更快,TraceMonkey 就会执行它,如果代码原生速度更快,JaegerMonkey 就会编译它,就像 SFX 一样。

It is browser-dependent. Look up WebKit's SquirrelFish Extreme and Google V8 to see what's at the fastest end of things, and look at Mozilla's JaegerMonkey for that implementation.

AFIAK V8 and SFX are JITs, so they compile JS code to native. JaegerMonkey and TraceMonkey combine in Firefox to form a system where if code would be faster traced, TraceMonkey executes it, and if code were faster native, JaegerMonkey compiles it, just like SFX.

吹泡泡o 2024-12-09 08:31:03

您有可以引用的句子来帮助理解上下文吗?

Javascript 在浏览器中编译(它以纯源代码发送到浏览器)。但它只有在加载时才会被编译。因此,如果您有一个 script 标签,后跟一个 div 标签,后跟一个 script 标签,那么它将按顺序加载这些内容。浏览器将停止加载整个页面(它仍然下载资源,只是不加载 HTML),直到加载脚本(这是因为脚本中可能包含“document.write”)。

<script>
var someVariable = 'hello world';
alert(document.getElementById('someid')); //alerts undefined
</script>

<div id='someid'></div>

<script>
alert(document.getElementById('someid')); //alerts 'someid'
alert(someVariable); //alerts 'hello world'
</script>

Do you have a sentence that you could quote to help with context?

Javascript is compiled at the browser (it's sent to the browser in plain source). But it only gets compiled as it is loaded. So if you have a script tag followed by a div tag followed by a script tag then it will load those things sequentially. The browser will stop loading the entire page (it still downloads resources, just doesn't load HTML) until your script has been loaded (this is because the script may have 'document.write' within it).

<script>
var someVariable = 'hello world';
alert(document.getElementById('someid')); //alerts undefined
</script>

<div id='someid'></div>

<script>
alert(document.getElementById('someid')); //alerts 'someid'
alert(someVariable); //alerts 'hello world'
</script>
怎樣才叫好 2024-12-09 08:31:03

JS 中有读取时和运行时(我喜欢这样想,因为它不是真正编译的,而是解释的)。听起来 O'Reilly 的书使用编译时作为读取时的同义词。

读取时间是指引擎读取所有代码并在全局范围内评估所有内容的时间。通常这会在将触发代码执行的事件上设置挂钩。

运行时就是一切。

There's read-time and run-time in JS (as I like to think of it, since it's not really compiled, but interpreted). It sounds like the O'Reilly book is using compile-time as a synonym for read-time.

Read-time is when the engine reads all of the code and evaluates everything at the global scope. Usually this sets up hooks on events that will trigger code execution.

Run-time is everything else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文