The global object in browsers

Maciej Stachowiak mjs at apple.com
Tue Feb 17 15:43:32 PST 2009


On Feb 17, 2009, at 2:02 PM, Ian Hickson wrote:

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

Actually it's the other way around - f() will return 1 and g() will  
return 2. (I think this might have been the reason Mark thought this  
example showed dynamic scoping; scope is purely lexical and reflects  
original global bindings, but a persistent handle to the global object  
reflects the new global object bought in by navigation.)

Regards,
Maciej



More information about the Es-discuss mailing list