The global object as the "global scope instance object"

Brendan Eich brendan at mozilla.org
Tue Jan 24 09:54:55 PST 2012


> Brendan Eich <mailto:brendan at mozilla.org>
> January 24, 2012 9:49 AM
> More fun:
>
> js> function f(k) {
>   for (var i = 0; i < k; i++) {
>     const j = i*i;
>     a.push(j);
>   }
> }
> js> a = []
> []
> js> f(10)
> js> a
> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Better example:

js> function f(k) {
   print(j);
   for (var i=0; i<k; i++) {
     const j = i*i;
     a.push(j);
     b.push(function(){return j});
   }
}
js> a = [], b = []
[]
js> f(10)
undefined
js> a
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
js> b[0]()
81

Again, none of this is for standardization. Quirky as heck, but we hope 
sane users treat SpiderMonkey const like top level ES6 const and won't 
be broken by an incompatible change to conform.

/be


More information about the es-discuss mailing list