JavaScript Scope: Execution Context
The Execution Context is an abstract representation of the current runtime environment in JavaScript. To explain a little better, the Execution Context is formed of three components.
- Lexical Environment
- Variable Environment
- This binding
- Scope chain
- Local variables
- ‘this’ keyword
The scope chain represents an array of parent ‘Variable environments’ up to and including the global variable environment.
Each Variable environment is in fact an object called an activation (or variable) object. Though not a real object in the normal sense, it does have properties which can be accessed.
{
__parent__ :[Object reference],
arguments: [Lots of fun that I wont go into now]
this: [Object reference]
mylocalVar: [undefined]
}
The above example represents the activation object after the initialisation of the current context, the myLocalVar property is a local variable. At this point the activation object is sometimes referred to as the Variable object, as far as I understand they are essentially the same thing (if for the moment we pretend the with() method doesn’t exist).
The ‘this’ property refers to the owner object (by default this is the global object .i.e. window).
The __parent__ property is a reference to the parent activation object. This object is not explicitly accessible, though access to properties on this object are implied from the scope chain.
The global context is some what confused, ‘this’ resolves to the activation object itself thus…
var foo = 'bar' console.log(window.foo); // bar console.log(this.foo); // bar