This is an article about debugging with JScript RuntimeObject (MSDN). Although most of these examples will not work in other browsers, they will work in IE 5.5+.
Leaked global identifier
For example, let's say you accidentally create a global property, such as:
function playRugby(players) {
var items,
i;
len = items.length;
// Global.
}
function kick() {
var x = 10
y = 11;
// As I makes y global.
}
When playRugby is called, the global property len is created, and if it does not already exist, the value of items.length is assigned to it. Likewise, when kick is called, the global property y is created.
None of these global variables are intentional. They break encapsulation and reveal implementation details. This can lead to conflicts and tricky dependency issues.
To detect these inadvertently created global identifiers, we can use a for in loop on the global object. Firebug's "DOM" tag provides this useful global detection.
Unfortunately, in IE, for in cannot enumerate any global variables and function declarations. Take a look at the following example:
// Property of global variable object.
var EX1_GLOBAL_VARIABLE = 10;
// Property of global object.
this.EX1_GLOBAL_PROPERTY = 11;
// Property of global variable object.
function EX1_GLOBAL_FUNCTION(){}
(function(){
var results = [];
for(var p in this) {
results.push(p);
}
alert("Leaked:n" + results.join("n"));
})();
In IE, the result contains a combination of window properties and one of four user-defined properties: EX1_GLOBAL_PROPERTY.
So, what happens to the other three user-defined properties? Why don't they show up in a for in loop.
It turns out that enumerating a global object enumerates the assigned global object properties but not the global variables.
Why global properties can be enumerated but global variables cannot. Experience tells us that JScript marks global variables (declared with var) as DontEnum. Since global objects are defined as global variable objects, this seems like a reasonable explanation. This is not standard but explains the behavior in IE. However, Eric Lippert proposed another explanation: global objects and global variable objects are two different objects in IE.
According to MS-ES3:
Variable declaration in JScript 5.x creates a property of the global object, which has the DontEnum attribute.