JavaScript 中的激活和变量对象?

发布于 2024-11-15 01:18:22 字数 116 浏览 3 评论 0原文

术语“激活对象”只是“变量对象”的另一个名称,还是它们之间实际上有什么区别?我一直在阅读一些关于如何在执行上下文中形成变量作用域的 JavaScript 文章,从我的角度来看,似乎在大多数文章中他们交替使用这两个术语。

Is the term "activation object" just another name of "variable object" or is there actually any difference between them? I have been reading a few JavaScript articles about how variable scopes are formed in an execution context, and from my point of view it seems that in most of the articles they use these two terms interchangeably.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鸠魁 2024-11-22 01:18:22

好吧,我刚刚学到了一些东西:)。从这篇文章来看,在函数的执行上下文中,激活对象用作变量对象:

创建执行上下文时,许多事情都会按定义的顺序发生。首先,在函数的执行上下文中,创建一个“Activation”对象。 [...]

然后使用 ECMA 262 称为“变量”对象的对象进行“变量实例化”过程。然而,Activation 对象被用作 Variable 对象(注意这一点,这一点很重要:它们是同一个对象)。为函数的每个形式参数创建 Variable 对象的命名属性,如果函数调用的参数与这些参数相对应,则这些参数的值将分配给属性(否则分配的值未定义)。

但是,当您处于全局范围内时,没有激活对象,因此全局对象将用作变量对象:

全局执行上下文的处理方式略有不同,因为它没有参数,因此不需要定义的激活对象来引用它们。 [...] 全局对象用作变量对象,这就是为什么全局声明的函数成为全局对象的属性。

因此,听起来“激活对象”和“变量对象”在函数上下文中是同一件事,但在全局上下文中却不是。

Well, I just learned something :). From this article, it would appear that within the execution context of a function, the Activation Object is used as the Variable Object:

When an execution context is created a number of things happen in a defined order. First, in the execution context of a function, an "Activation" object is created. [...]

Then the process of "variable instantiation" takes place using an object that ECMA 262 refers to as the "Variable" object. However, the Activation object is used as the Variable object (note this, it is important: they are the same object). Named properties of the Variable object are created for each of the function's formal parameters, and if arguments to the function call correspond with those parameters the values of those arguments are assigned to the properties (otherwise the assigned value is undefined).

However, when you're in the global scope, there isn't an Activation Object, so the Global Object is used as the Variable Object instead:

The global execution context gets some slightly different handling as it does not have arguments so it does not need a defined Activation object to refer to them. [...] The global object is used as the Variable object, which is why globally declared functions become properties of the global object.

So it sounds like "Activation Object" and "Variable Object" are the same thing within a function context, but not within the global context.

宣告ˉ结束 2024-11-22 01:18:22

激活对象作用域链中最上面的对象,最下面的是全局对象
变量对象是抽象概念,因此根据其执行上下文,可以是作用域链中的任何链接,包括激活/全局对象


它包含:

  • 您在函数体内声明的所有变量和函数
  • 参数命名由函数签名指定;
  • 参数作为名为 arguments对象(如果您希望函数支持多个签名)。

它不包含:

  • this (因为它不是变量);
  • 命名函数表达式

更多信息 - JavaScript。核心。

tl;dr 的情况下很少引用:

变量对象是与执行上下文相关的数据范围。它是一个与上下文关联的特殊对象,它存储在上下文中定义的变量和函数声明。

变量对象是一个抽象概念。在不同的上下文类型中,物理上,它使用不同的对象来呈现。

[..]在全局上下文中,变量对象全局对象本身[..]

[..]函数的变量对象是相同的简单变量对象,但除了变量函数声明,它还存储形式参数参数对象,称为激活对象

[..] 在代码中访问 this 时,其值直接从执行上下文获取,无需任何作用域链查找.

An activation object is the uppermost object in a scope-chain with the lowermost being global object.
Whereas variable object is abstract concept and therefore, depending on its execution context, is any link in scope-chain including activation/global object.


It contains:

  • all the variables and functions you declare inside the function body;
  • arguments named as specified by the function signature;
  • arguments as an object named arguments (in case you want your function to support multiple signatures).

It doesn't contain:

  • this (as it's not a variable);
  • named function expressions.

Further info - JavaScript. The core.

Few quotes in case of tl;dr:

A variable object is a scope of data related with the execution context. It’s a special object associated with the context and which stores variables and function declarations are being defined within the context.

A variable object is an abstract concept. In different context types, physically, it’s presented using different object.

[..] in the global context the variable object is the global object itself [..]

[..] a function’s variable object is the same simple variable object, but besides variables and function declarations, it also stores formal parameters and arguments object, and is called the activation object.

[..] when accessing this in a code, its value is taken directly from the execution context without any scope-chain lookup.

云巢 2024-11-22 01:18:22

更准确地说,Activation 对象是 Variable 对象的一种类型。这类似于男人是人类的一种。正如此处所述,术语“变量对象”只是一个广义的用于描述任何对象的术语,该对象拥有描述当前执行上下文的环境和范围的属性。

因此,在全局执行上下文中(即在任何函数之外),它最终成为全局对象。为什么?因为它是保存描述全局执行上下文的环境和范围的属性的对象。

而在函数本地执行上下文中(即在函数内),函数本地对象(也称为激活对象)是变量对象,因为它是保存描述当前环境和范围的属性的对象。执行函数。例如函数参数等属性。

It's more accurate to say that an Activation object is a type of Variable object. This is similar to how a man is a type of HUMAN. As stated here, the term 'Variable object' is just a GENERALISED term used to describe any object that holds the properties that describe the environment and scope of the currently executing context.

Hence, within the global executing context (i.e., outside of any functions), it ends up being the Global object. Why? Because it’s the object that holds the properties that describe the environment and scope of the global executing context.

Whereas within the function local executing context (i.e., within a function), it is the function local object (a.k.a the Activation object) that is the Variable object, as it's the object that holds the properties that describe the environment and scope of the currently executing function. Properties such as function arguments for example.

囍孤女 2024-11-22 01:18:22

激活的对象仅意味着代表发生事件的网页上的元素的对象。因此,如果单击图像,表示该图像的 JavaScript 对象就是激活的对象。

An activated object just means an object that represents an element on a web page that an event occurred on. So if an image is clicked, the JavaScript object that represents that image is the activated object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文