The global object in browsers

Ian Hickson ian at
Tue Feb 17 14:02:30 PST 2009

Right now ES3 assumes that there is a single global object, which is used 
at the top of the scope chain and that is returned for "this" in the 
global scope.

It is possible to show that this is now what some browsers do:

   var x = 1;
   function f() { return x; }
   var global = this;
   function g() { return global.x; }

   // some other page's script takes references to f and g
   // browser navigates to a new page

   var x = 2;

Now, if the other page's script calls f() and g(), it will get different 
results (2 and 1 respectively, if I didn't screw up the example code).

For HTML5, this behaviour has been defined in more detail. The global 
object is a Window object. This object is per-Document. The object 
returned by the "window" attribute on that global object is actually a 
WindowProxy object, which forwards everything to the "current" Window 
object. However, doing this has required that I require browsers to 
violate the requirement that the ES3 spec has, namely that "this" and the 
object at the top of the scope chain are both the global object, because 
in this model an invariant is that script cannot access the actual global 
object directly, only the proxy. The HTML5 spec says:

   If the script's global object is a Window object, then in JavaScript, 
   the this keyword in the global scope must, contrary to the ECMAScript 
   specification, return the Window object's WindowProxy object.

If it would be possible for the ECMAScript specification to have a hook 
that allowed me to require this without violating the spec, that would be 

Ian Hickson               U+1047E                )\._.,--....,'``.    fL       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

More information about the Es-discuss mailing list