سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

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.

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top