Real World Func Decl in Block Scope Breakages

Brendan Eich brendan at mozilla.com
Wed Dec 26 21:50:45 PST 2012


[[bcc'ing es-discuss since this is a tangent.]

Well, bcc'ing didn't work -- mailman does not respect. Resending for 
posterity.

/be]

Brandon Benvie wrote:
> if I run this code in the tools > web developer > web console, I get a 
> result of undefined and no error
> `if (false) { function x(){ console.log('worked') } } x();`

Best to test other ways than consoles, they do odd things in some 
browsers. But what you see is what I see when I load the content wrapped 
in script tags from /tmp/foo.html as well:

<script>
     if (false) { function x(){ console.log('worked') } }
     x()
</script>

So Firefox is eating the error just as the console is. Not so a 
SpiderMonkey shell:

js> if (false) { function x(){ console.log('worked') } } x()
typein:1:53 ReferenceError: x is not defined

Let's try onerror:

<script>
     this.onerror = function(s) { alert('onerror: ' + s); }
     if (false) { function x(){ console.log('worked') } }
     x()
</script>

Not sure what's up with the non-error, but the REPL shows a 
ReferenceError as expected. Could you please file a bug cc'ing me, 
dmandelin at mozilla.com, and :bz? Thanks.

/be

>
>
> On Wed, Dec 26, 2012 at 9:06 PM, Brendan Eich <brendan at mozilla.com 
> <mailto:brendan at mozilla.com>> wrote:
>
>     Brandon Benvie wrote:
>
>         My tests basically indicate what you said, but with a minor
>         distinction. They don't fail outright, as in throw an error.
>         They just fail silently. As in, it's ok to call the
>         identifier, but it's not going to do anything.
>
>
>     No, something else is wrong. Can you show such a (reduced, I take
>     it) testcase?
>
>
>         This is a curious Schrodinger's cat kind of intermediate
>         state, where it doesn't succeed in doing what it was asked,
>         but doesn't throw an error either, which in JS terms means it
>         air-quotes succeeded but didn't really.
>
>
>     SpiderMonkey's function-in-block does not create any such magic
>     error-free behavior. I don't know what you're seeing, but it's not
>     what I saw at a glance, and not what SpiderMonkey implements for
>     f-i-b.
>
>     /be
>
>


More information about the es-discuss mailing list