Javascript 是编译还是两遍解释?
我是一名公认的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它依赖于浏览器。查看 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.
您有可以引用的句子来帮助理解上下文吗?
Javascript 在浏览器中编译(它以纯源代码发送到浏览器)。但它只有在加载时才会被编译。因此,如果您有一个 script 标签,后跟一个 div 标签,后跟一个 script 标签,那么它将按顺序加载这些内容。浏览器将停止加载整个页面(它仍然下载资源,只是不加载 HTML),直到加载脚本(这是因为脚本中可能包含“document.write”)。
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).
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.