Skip to content

JavaScript Scope: Execution Context

10 July, 2011

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

Perhaps a better way to think of this is:
  • 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

Advertisement
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.