Okay, so I lately have been reading about ES-5 lexical environment scope and I am not sure if I really understand what is going on with how variables are stored in EcmaScript. I did some research but it didn't clarified my information, only brought me up to two questions. So there they are:
The first one is about ES-3
activations objects
/variable objects
. After reading ES-3 Specification and some sources on the Internet I can assume that they are just normal object, for example like those created bynew Object
, but none of the sources says "yes, this is just a plain object" directly. Moreover, Dmitry Soshnikov wrote on his blog (the emphasis is mine):Schematically and for examples, it is possible to present variable object as a normal ECMAScript object
and that quotation doesn't let me be sure about what really an activation object is. So this is the first question: is an activation object a regular EcmaScript object? If not, then what is it otherwise?
In ES-5 we now have
object environment records
, which seem to be more or less the same that ES-3 activation objects, anddeclarative environment records
, which replaced activation objects in functions and try-catch statement. So, assuming that object environment records are just plain EcmaScript objects, then what is a declarative environment record? The specification doesn't clarify this, furthermore, from what I've read there I cannot imagine that this is not implemented as an object. So, again, if declarative environment records are not ES objects, then what are they and how are they implemented and represented during the code execution?
Thank you very much in advance for brightening that topic for me.
EDIT: I think I need to clarify what is this question about. The main thing that I want to know is what is the exact difference between activation objects/object environment records and declarative environment records. That's what I'm interested in most.
First of all you have to be aware that all of these terms just describe concepts. They don't dictate any kind of implementation. But because this can be hard to imagine/visualize it can be helpful to think about these concepts as instantiations of something you know, like maps or tables.
Declarative environment records (DER) and object environment records (OER) have one thing in common: They are both environment records (ER), which are defined in the specification as:
This basically means that an ER keeps track of variable and function names and their associated values.
Consider this example:
The corresponding ER would have two entries, one for
foo
and one forbar
. If you imagine an ER to be a table, then it would look likeNow on to the difference between DER and OER. A DER might be the easiest to understand.
Declarative Environment Record
The term declarative should sound familiar since we are often talking of variable declarations and function declarations. The specification says:
So, when you see
or
then you can assume that their names and values are stored in a DER.
Object Environment Record
OERs are less common, but in each JS application there exist at least one OER. The specification describes it as
Have you ever wondered why the properties of the
window
object are global variables? That's because the ER of the global scope is an OER:window
is the binding object and for each of its properties a corresponding entry is created in the OER. This is also in the specification:Here is an example: Lets assume out binding object is
then the OER would be
Note that in this case, the binding object (
obj
) is really a JavaScript object. You are in the same situation when you are using thewith
statement:with
creates a OER and populates it with the property names and values from the passed object. That's why you can access them without explicitly referring to them viaobj.*
. The OER also makes sure to update the binding object with the new value if one was assigned to one of the identifiers.Activation Object
It looks like that in ES3, activation objects (AO) where automatically created when a function was executed and it was holding a reference to the special
arguments
object. This seems to be related to DERs, but still to be something independent.The concept of AOs doesn't seem to exist anymore in ES5 and I assume that it was unnecessary, since
arguments
can be added directly to the DER of the execution context.Execution Context
A new execution context (EC) is established whenever a function is executed and is used to keep track of the state of the execution:
This means the engine can add whatever information it needs to track the execution progress. But the specification also defines components that an EC must have, one of which is the VariableEnvironment, which is an ER (probably always a DER, but I don't know for sure). That means an ER is a part of an EC.