>
> Inside the catch, the catch-scope is first for reading and writing.
> But the catch scopes are ignored for declaring new variables. So your
> expectation seems to be the correct one.


That was my analysis as well.

§10.5 tells us that `var` creates bindings in the anonymous function's
environment, so the function's env has `e`, `o`, and `u` bindings.

§12.14 tells us that within the `catch`, a new environment is created with
the function's environment being the parent. So the assignment to `e` is to
the `catch`'s `e`, not the function's.

Annotating that code a bit:

(function(){
    var e; // Where these actually happen
    var o;
    var u;

    try {
        throw "hi";
    } catch (e) {
        e = "ho"; // Assigns to the `catch` env's `e`
        o = "hu"; // Assigns to the function's `o`
        console.log(e);
    }
    console.log(e, u, o); // Expect undefined undefined "hu"

}());

-- T.J.


On 14 May 2012 11:11, Peter van der Zee <[email protected]> wrote:

> On Mon, May 14, 2012 at 11:57 AM, Claus Reinke <[email protected]>
> wrote:
> > What should be the output of the following code?
> >
> > (function(){
> >
> > try {
> >  throw "hi";
> > } catch (e) {
> >  var e = "ho";
> >  var o = "hu";
> >  var u;
> >  console.log(e);
> > }
> > console.log(e,u,o);
> >
> > }());
> >
> > It seems clear that the first console.log should output 'ho'.
> > Implementations seem to disagree on the second console.log, though.
> >
> > From my current understanding of the spec, I expected:
> >   undefined undefined 'hu'
> >
>
> Inside the catch, the catch-scope is first for reading and writing.
> But the catch scopes are ignored for declaring new variables. So your
> expectation seems to be the correct one. `e` is created in the scope
> of the anonymous function. Likewise, `o` and `u` are created in that
> scope too (so neither throw at the second console.log). "ho" is
> assigned to the catch-scope `e`, since that's the first scope in the
> scope traversal lookup at that point. Catch scopes are weird, yo.
>
> - peter
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to