全局执行上下文是否可以弹出执行堆栈?
Is it possible for Global Execution Context to pop off the execution stack?
当 JS 代码开始 运行 时,全局执行上下文被创建并位于执行堆栈的底部,至于 "accomodate" 全局变量对象和 'this'
。
如果在整个JS代码运行并且没有全局执行上下文之后执行堆栈变空,我们如何仍然能够访问全局variables(例如,我正在 运行ning 一个带有 JS 代码的 html 文件,完成后,我仍然能够看到全局变量的值通过 Chrome 控制台...)或 this
如何仍然指向全局对象 (不应该有任何'this'
如果没有任何执行上下文!)?
我可能给自己的唯一解释是全局执行上下文永远不会离开执行堆栈;它一直存在,直到我决定关闭浏览器 window。 我说的对不对?
此外,在异步回调的情况下,当事件离开事件队列并进入JS引擎运行时,执行堆栈中到底发生了什么?回调的执行上下文是位于堆栈底部还是全局执行上下文仍然存在?
有相似主题;但是,它没有回答我的问题。
谢谢
当整个代码为 运行.
时,执行堆栈变空
how are we still able to access the global variables?
即使没有执行任何代码 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,其词法和变量环境设置为全局环境并且 this
绑定到全局对象。然后你的代码在这个上下文中执行,执行堆栈再次变空。
how this
still points to global object?
每次使用全局代码初始化新的全局执行上下文时,this
都会绑定到全局对象。
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 this is described in slightly different terms than in ECMAScript spec:
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. (MDN. Concurrency model and event loop)
这里"stack frame"表示"execution context","initial stack frame"对应"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 个。堆栈是空的。并且只有当它为空时,才会从 callback/event 队列中获取最早的回调:
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. ECMAScript 6.0 spec
当 JS 代码开始 运行 时,全局执行上下文被创建并位于执行堆栈的底部,至于 "accomodate" 全局变量对象和 'this'
。
如果在整个JS代码运行并且没有全局执行上下文之后执行堆栈变空,我们如何仍然能够访问全局variables(例如,我正在 运行ning 一个带有 JS 代码的 html 文件,完成后,我仍然能够看到全局变量的值通过 Chrome 控制台...)或 this
如何仍然指向全局对象 (不应该有任何'this'
如果没有任何执行上下文!)?
我可能给自己的唯一解释是全局执行上下文永远不会离开执行堆栈;它一直存在,直到我决定关闭浏览器 window。 我说的对不对?
此外,在异步回调的情况下,当事件离开事件队列并进入JS引擎运行时,执行堆栈中到底发生了什么?回调的执行上下文是位于堆栈底部还是全局执行上下文仍然存在?
有相似主题
谢谢
当整个代码为 运行.
时,执行堆栈变空how are we still able to access the global variables?
即使没有执行任何代码 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,其词法和变量环境设置为全局环境并且 this
绑定到全局对象。然后你的代码在这个上下文中执行,执行堆栈再次变空。
how
this
still points to global object?
每次使用全局代码初始化新的全局执行上下文时,this
都会绑定到全局对象。
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 this is described in slightly different terms than in ECMAScript spec:
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. (MDN. Concurrency model and event loop)
这里"stack frame"表示"execution context","initial stack frame"对应"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 个。堆栈是空的。并且只有当它为空时,才会从 callback/event 队列中获取最早的回调:
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. ECMAScript 6.0 spec