Skip to content

Instantly share code, notes, and snippets.

@Integralist
Created December 27, 2011 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/1525419 to your computer and use it in GitHub Desktop.
Save Integralist/1525419 to your computer and use it in GitHub Desktop.
Execution context within JavaScript (Variable/Activation Object) shortened from @kangax's post on 'Understanding delete'

Reference: http://perfectionkills.com/understanding-delete/

There are 3 types of executable code in ECMAScript:

  • Global code
  • Function code
  • Eval code

When ECMAScript code executes, it always happens within a certain execution context.

Execution contexts can logically form a stack. First there might be Global code with its own execution context; that code might call a function, with its own execution context; that function could call another function, and so on and so forth. Even if function is calling itself recursively, a new execution context is being entered with every invocation.

Global code (and the Variable Object)

Every execution context has a so-called Variable Object associated with it.

When control enters execution context for Global code, a Global object is used as a Variable object. This is precisely why variables or functions declared globally become properties of a Global object.

Function code

Ok, so global variables become properties of Global object, but what happens with local variables — those declared in Function code? The behavior is actually very similar: they become properties of a Variable object. The only difference is that when in Function code, a Variable object is not a Global object, but a so-called Activation object. An Activation object is created every time execution context for Function code is entered.

Not only do variables and functions declared within Function code become properties of Activation object; this also happens with each of function arguments (under names corresponding to formal parameters) and a special Arguments object (under arguments name). Note that Activation object is an internal mechanism and is never really accessible by program code.

Eval code

Variables declared within Eval code are created as properties of calling context’s Variable object. Eval code simply uses Variable object of the execution context that it’s being called within.

Property Attributes

Every property can have zero or more attributes from the following set — ReadOnly, DontEnum, DontDelete and Internal. You can think of them as flags — an attribute can either exist on a property or not. For the purposes of today’s discussion, we are only interested in DontDelete.

When declared variables and functions become properties of a Variable object — either Activation object (for Function code), or Global object (for Global code), these properties are created with DontDelete attribute. However, any explicit (or implicit) property assignment creates property without DontDelete attribute. And this is essentialy why we can delete some properties, but not others.

Summary

Here’s a short summary of how deletion works in Javascript:

  • Variables and function declarations are properties of either Activation or Global objects.
  • Properties have attributes, one of which — DontDelete — is responsible for whether a property can be deleted.
  • Variable and function declarations in Global and Function code always create properties with DontDelete.
  • Function arguments are also properties of Activation object and are created with DontDelete.
  • Variable and function declarations in Eval code always create properties without DontDelete.
  • New properties are always created with empty attributes (and so without DontDelete).
  • Host objects are allowed to react to deletion however they want.
@iampals
Copy link

iampals commented Jan 5, 2013

nice explanation

@bb-swapnil
Copy link

good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment