« Return to Thread: The global object in browsers

The global object in browsers

by Ian Hickson :: Rate this Message:

Reply to Author | View in Thread


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
great.

--
Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'
_______________________________________________
Es-discuss mailing list
Es-discuss@...
https://mail.mozilla.org/listinfo/es-discuss

 « Return to Thread: The global object in browsers