javascript – 全局执行上下文是否可以弹出执行堆栈?

前端之家收集整理的这篇文章主要介绍了javascript – 全局执行上下文是否可以弹出执行堆栈?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当JS代码开始运行时,全局执行上下文被创建并位于执行堆栈的底部,以“容纳”全局变量对象和’这’.
如果在执行整个JS代码并且没有全局执行上下文后执行堆栈变空,那么我们如何仍然能够访问全局变量(例如,我正在运行带有JS的html文件)代码和完成后,我仍然能够通过Chrome控制台看到全局变量的值…)或者它如何仍然指向全局对象(如果没有任何执行上下文,则不应该有任何’this’ !)?

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

此外,在异步回调的情况下,当一个事件离开事件队列并进入JS引擎运行时,执行堆栈中究竟发生了什么?回调的执行上下文是否位于此堆栈的底部,还是全局执行上下文仍然存在?

有一个类似的主题Is the initial global execution context ever popped off the call stack in JavaScript?;但是,它没有回答我的问题.

谢谢

解决方法

运行整个代码时,执行堆栈变空. @H_403_15@

how are we still able to access the global variables?

即使没有执行代码,the global lexical environment仍然存在全局对象.当你喂一些代码到镀铬的控制台,代码正在评估,新的全球execution contextbeing created and initialized设置为全球环境及其词汇和可变的环境,这个绑定到全局对象.然后,您的代码在此上下文中执行,执行堆栈再次变为空.

@H_403_15@

how this still points to global object?

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

@H_403_15@

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 spec略有不同的描述:

@H_403_15@

When the stack is empty,a message is taken out of the queue and processed. The processing consists of calling the associated function (and thus creating an initial stack frame). The message processing ends when the stack becomes empty again. (07005)

这里“堆栈帧”表示“执行上下文”,“初始堆栈帧”对应于“全局执行上下文”.

@H_403_15@

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

他们都不是.堆栈是空的.并且只有它是空的,最旧的回调是从回调/事件队列中获取的:

@H_403_15@

When there is no running execution context and the execution context stack is empty,the ECMAScript implementation removes the first PendingJob from a Job Queue and uses the information contained in it to create an execution context and starts execution of the associated Job abstract operation. 07006

原文链接:https://www.f2er.com/js/151219.html

猜你在找的JavaScript相关文章