Friday afternoon scoping quiz

P T Withington ptw at pobox.com
Fri Feb 5 14:14:33 PST 2010


({ 
  'foo': 20, 
  'test': function () { 
    var f = function () { 
      alert("foo:"+ foo);
      alert("this.foo:"+ this.foo);
      alert("bar:"+ bar);
    } 
    with (this) { 
      var foo = 42; 
      var bar = 21; 
      f.call(this); 
    } 
  } 
}).test();

--

By my reading, `foo` and `bar` are declared and initialized in `test`, and closed over by `f`.  I expect to see:

foo:42
this.foo:20
bar:21

But in Rhino, Firefox, Safari and Opera I am seeing:

foo:undefined
this.foo:42
bar:21

Flash 10 gives me the answer I expected.  I have not tested other JS engines.




More information about the es-discuss mailing list