JavaScript中的执行上下文 [英] Execution contexts in JavaScript

查看:85
本文介绍了JavaScript中的执行上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为JavaScript中的每个函数创建一个新的执行上下文。

A new execution context is created for each function in JavaScript.

运行以下代码时,内存中有多少执行上下文?请注意,函数 Bar 未被调用。

How many execution contexts are present in memory when the following code is run? Note that function Bar is not invoked.

function Foo () {

  function Bar() {}

}

Foo();

此外,何时创建执行上下文?在评估时或运行时?

Also, when are the execution contexts created? At evaluation time or runtime?

推荐答案

函数的运行时调用是导致创建执行上下文的原因。因此,在您的示例中,只有一个函数调用,因此只涉及一个执行上下文。

The runtime invocation of a function is what causes an execution context to be created. In your example, therefore, there's only one function call, so only one execution context is involved.

函数的静态(编译时)排列很重要,因为确定执行上下文的范围和最终内容。然而,实际调用函数对于创建上下文很重要。 (一些较旧的语言使用术语激活记录,尽管这可能更适用于基于堆栈的分配。)

The static (compile-time) arrangement of functions is important, because that determines scope and the eventual contents of execution contexts. It's the actual call to a function that matters, however, for the creation of a context. (Some older languages used the term "activation record", though that may have been more intended for stack-based allocations.)

您可以阅读有时候使用某种语言的详细信息规范,尽管很难弄清楚森林里的树木。规范是根据转移的控制来编写的。函数调用是一种非常常见的方式,但是事件处理程序的调用,或者最初调用完整的< script> 块时也是如此。由浏览器加载。

You can read details in the sometimes turgid language of the spec, though it can be hard to make out the forest for the trees. The spec is written in terms of control being transferred. A function call is a very common way that that happens, but so is the invocation of an event handler, or the invocation of a complete <script> block when it is initially loaded by a browser.

这篇关于JavaScript中的执行上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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