On Mon, May 14, 2012 at 3:25 AM, T.J. Crowder <t...@crowdersoftware.com> wrote:
>> 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"

I believe that -- if we had any practical way to measure this --
'most' web developers would expect 'ho undefined hu'. This is the
sensible output, the one given by ie9. It is sensible because the
simple synopsis of the funky var rules is "variables get values when
assignments are executed".  In my opinion the spec-ed answer here is
less useful to developers than the ie9 answer.  It may be true that
specifications which allowed the sensible answer would result in other
more serious nonsense of course.

jjb


>
> }());
>
> -- T.J.
>
>
> On 14 May 2012 11:11, Peter van der Zee <e...@qfox.nl> wrote:
>>
>> On Mon, May 14, 2012 at 11:57 AM, Claus Reinke <claus.rei...@talk21.com>
>> 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
>> es-discuss@mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to