全局执行上下文是否有可能弹出执行堆栈? [英] Is it possible for Global Execution Context to pop off the execution stack?

查看:137
本文介绍了全局执行上下文是否有可能弹出执行堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当JS代码开始运行时,将创建全局执行上下文并将其放置在执行堆栈的底部,以容纳"全局变量对象和'this'. 如果在整个JS代码运行之后执行堆栈变空并且没有全局执行上下文的情况下, 我们如何仍然能够访问全局变量 (例如,我正在运行带有JS代码的html文件,完成后,我仍然可以通过Chrome控制台查看全局变量的值...)或 this的运行方式指向全局对象 (如果没有任何执行上下文,则不应有任何'this'!)?

When JS code starts to run, the Global Execution Context is created and sits at the bottom of the execution stack as to "accomodate" Global Variable Object and'this'. If it is the case for the execution stack to get empty after the whole JS code is run and there is no Global Execution Context, how are we still able to access the global variables (for example, I am running an html file with its JS code and after its completion, I am still able to see the values of global variables through Chrome console...) or how this still points to global object (there shouldn't be any 'this' if there wasn't any Execution Context!)?

我可能会给自己的唯一解释是,全局执行上下文永远不会离开执行堆栈.它一直存在,直到我决定关闭浏览器窗口. 我对吗?

此外,在异步回调的情况下,当事件退出事件队列并进入要运行的JS引擎时,执行堆栈中到底发生了什么?回调的执行上下文位于该堆栈的底部还是全局执行上下文仍在那?

Moreover, in case of asynchronous callbacks, when an event gets out of the event queue and gets into JS engine as to be run, what exactly happens in the execution stack? Is the callback's execution context sitting at the bottom of this stack or the global execution context is still there?

有一个类似的主题

There is a similar subject Is the initial global execution context ever popped off the call stack in JavaScript?; however, it does not answer my questions.

谢谢

推荐答案

当整个代码运行时,执行堆栈为空.

The execution stack gets empty when the whole code is run.

我们如何仍然能够访问全局变量?

how are we still able to access the global variables?

即使没有执行任何代码,全球词汇环境全局对象仍然存在.当您将某些代码输入chrome控制台时,正在评估代码,新的全局执行上下文已创建且已初始化,并且已设置了词法和可变环境到全局环境,this绑定到全局对象.然后,您的代码将在此上下文中执行,并且执行堆栈再次变空.

Even when no code is executed the global lexical environment with the global object still exists. When you feed some code into chrome console, the code is being evaluated, a new global execution context is being created and initialized with its lexical and variable environments set to the global environment and this bound to the global object. Then your code is executed within this context and the execution stack gets empty again.

this如何仍然指向全局对象?

how this still points to global object?

每次使用全局代码初始化新的全局执行上下文时,this都会绑定到全局对象.

Every time a new global execution context is initialized with the global code, this gets bound to the global object.

在异步回调的情况下,当事件退出事件队列并进入要运行的JS引擎时,执行堆栈中到底发生了什么?

in case of asynchronous callbacks, when an event gets out of the event queue and gets into JS engine as to be run, what exactly happens in the execution stack?

同样,将创建一个新的全局执行上下文并将其推送到空的执行堆栈中.在 MDN 中,这与ECMAScript规范:

Again, a new global execution context is created and pushed onto the empty execution stack. In MDN this is described in slightly different terms than in ECMAScript spec:

当堆栈为空时,将从队列中取出一条消息并进行处理.该处理包括调用关联的函数(并因此创建初始堆栈帧).当堆栈再次变空时,消息处理结束. ( MDN.并发模型和事件循环 )

此处堆栈框架"表示执行上下文",初始堆栈框架"对应于全局执行上下文".

Here "stack frame" means "execution context" and "initial stack frame" corresponds to "global execution context".

回调的执行上下文位于此堆栈的底部还是全局执行上下文还在?

Is the callback's execution context sitting at the bottom of this stack or the global execution context is still there?

没有一个.堆栈为空.而且只有当它为空时,最旧的回调才从回调/事件队列中获取:

None of them. The stack is empty. And only if it is empty, the oldest callback is taken from the callback/event queue:

当没有正在运行的执行上下文并且执行上下文堆栈为空时,ECMAScript实现将作业队列中的第一个PendingJob删除,并使用其中包含的信息来创建执行上下文并开始执行关联的Job抽象操作. ECMAScript 6.0规范

这篇关于全局执行上下文是否有可能弹出执行堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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