在 javascript V8 中,编译阶段在执行阶段之前发生在函数上,然后执行所有代码或仅针对全局上下文 [英] In javascript V8 does compilation phase happen to functions before execution phase then all the code is executed or only for global context

查看:29
本文介绍了在 javascript V8 中,编译阶段在执行阶段之前发生在函数上,然后执行所有代码或仅针对全局上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过很多文章说编译(创建)阶段首先发生在全局执行上下文然后执行代码,当一个函数被调用时,创建阶段然后再次为这个函数开始,然后它被执行这真的是正在发生的事情吗?为什么所有代码​​都没有发生创建阶段,首先包括函数内部的变量,然后所有代码都被执行,因为这样创建阶段是执行阶段本身的一部分,引擎在执行之前不知道函数内部的哪些变量阶段此外,如果是这样,为什么这样的事情会在没有先登录控制台的情况下出现错误

I read many articles saying that compilation(creation) phase happens first to the global execution context then the code is executed and when a function is invoked the creation phase then begins again for this function and then it is executed is this really what is happening? why creation phase is not happening for all of the code first including variables inside functions then all of the code is executed because like this the creation phase is part of the execution phase itself and the engine do not know what variables inside the function until the execution phase Also if so why something like this gives error without logging to the console first

console.log('First No Error')

function Error() {
  let a = 2
  console.log(z)
}

Error()

它给出了参考错误,因为没有先登录到控制台(没有错误)就没有定义 z为什么会发生这种情况,因为引擎不应该知道函数内部存在错误,直到它只在最后一行执行.

It gives reference error as z is not defined without logging to the console (No Error) first why this is happening as the engine should not know that there is an error inside a function until it is executed at the last line only.

我想知道函数内部已知的内容,并且可以在函数本身执行之前使用它(即使它只是一个没有内存中真实数据的引用,并且当它成为内存中的真实数据时).

I want to know what is known inside the function and one can use it(even if it just a reference without real data in memory and when it becomes real data in memory) before the function itself is executed.

推荐答案

(此处为 V8 开发人员.)

(V8 developer here.)

编译(创建)阶段

这是两种不同的东西.创建阶段"是一些人提出的一个概念,目的是向其他人(比如你)解释 JavaScript 引擎的作用.如果您发现它更令人困惑而不是有帮助,您可能应该将反馈直接发送给他们:-)

Those are two different things. The "creation phase" is a concept that some people have come up with in order to explain to other people (like you) what a JavaScript engine does. If you find it more confusing than helpful, you should probably direct that feedback at them :-)

编译"是引擎内部的实现细节:一些引擎可能会将 JavaScript 源代码编译为字节码或机器码或两者兼而有之,而其他引擎则可能不会;JavaScript 语言规范对此没有意见.你在现代浏览器中发现的 JavaScript 引擎都做各种形式的编译和重新编译;细节取决于每个引擎,并且不时发生变化.在基于编译思想构建的引擎中,编译必须在执行之前发生(因为它是将被执行的编译结果),但多长时间并不重要:它可能发生在第一次执行之前,或者很久以前.

"Compilation" is an engine-internal implementation detail: some engines might compile JavaScript source to bytecode or machine code or both, others might not; the JavaScript language specification has no opinion on this. The JavaScript engines you find in modern browsers all do various forms of compilation and recompilation; the details are up to each engine and change every now and then. In an engine that's built on the idea of compilation, compilation has to happen before execution (because it's the result of the compilation that will get executed), but it doesn't matter how long: it could happen right before the first execution, or a long time before it.

JavaScript 规范确实要求引擎在看到代码时立即报告某些错误(早期错误").因此引擎必须立即查看所有代码,至少要找到这些类型的错误.但这与编译任何东西都不一样.(而且 console.log(z) 不是早期错误的例子.)

The JavaScript specification does require engines to report certain errors ("early errors") immediately when seeing the code. So engines do have to look at all the code right away, at least to find these kinds of errors. But that's not the same as compiling anything. (And console.log(z) is not an example of an early error.)

JavaScript 引擎喜欢将任何不需要的工作推迟到以后,以保持快速启动.让网站加载得更快是一种更好的用户体验,而且由于页面加载通常只涉及页面的一些资源(例如:只有一些 JS 函数被调用,只有一些图像被显示),浏览器可以加快速度的一种方式页面加载只执行加载所需的操作:像编译那些只会在以后调用的函数,以及下载那些只会在以后显示的图像之类的工作,可以推迟到实际需要时.

JavaScript engines like to postpone any work that isn't needed yet until later in order to keep startup fast. Having web sites load faster is a better user experience, and since page load typically only involves some of the page's resources (e.g.: only some of the JS functions get called, only some of the images are displayed), one way how browsers can speed up page load is by only doing what's necessary for the load: work like compiling those functions that will only get called later, and downloading those images that will only get shown later, can be postponed until it is actually needed.

它给出了参考错误,因为没有首先登录到控制台(无错误)就没有定义 z,这是为什么发生这种情况

It gives reference error as z is not defined without logging to the console (No Error) first why this is happening

这不是正在发生的事情;首先没有错误"在引发 ReferenceError 之前记录.试试看!

That is not what's happening; "First No Error" is logged before the ReferenceError is thrown. Try it and see!

我想知道函数内部的已知内容,并且可以在函数本身执行之前使用它.

I want to know what is known inside the function and one can use it before the function itself is executed.

在相应的代码运行时创建对象并初始化变量.当您定义一个函数时,您可以引用该函数外部(词法)范围内的任何变量.例如:

Objects are created and variables are initialized when the respective code runs. When you define a function, you can refer to any variables in the function's outer (lexical) scope. For example:

function f1() {
  console.log(a);  // This will be fine.
  console.log(b);  // This will be an error (at execution time).
  console.log(c);  // This will be an error (at execution time).
  console.log(d);  // This will log 'undefined'.
}

// This is in f1's outer scope, so f1 can use it.
// For better readability, I would recommend to define variables like `a`
// before defining the functions that use them (like f1), but that's not
// a JavaScript requirement, just a recommendation for human readers.
var a = 42;

function f2() {
  var b = 123;  // This is not in f1's outer (lexical) scope.
  f1();
}
f2();

// This is in f1's outer scope, but only created after f1 is called.
// Contrary to `var` variables, `let` variables are not hoisted to the
// beginning of their scope, so are (sort of) "invisible" before.
let c = 77;
// f1 will be able to see the existence of this variable, but its value
// will only be set after the call (when execution reaches this point here),
// so within f1 it will be `undefined`.
var d = 88;

这篇关于在 javascript V8 中,编译阶段在执行阶段之前发生在函数上,然后执行所有代码或仅针对全局上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆