Hoisting behaviour of 'const' and 'let'

Yuh-Ruey Chen maian330 at gmail.com
Sun Oct 12 14:32:42 PDT 2008


David-Sarah Hopwood wrote:
> Yuh-Ruey Chen wrote:
> > Then what about this example?
> > 
> > // global scope
> > if (cond)   // suppose it's true
> >     y = 'f';
>
> I think you meant
>       y = f;
>   

No, I mean what is written: y is assigned the string "f". The example
would not make any sense if it were not a string. |this[y]| really
implies that y has a string.

> > this[y]();
> > const x = 3;
> > function f() { ... x ... }
> > 
> > This is a case where static analysis would not find the premature call
> > to f().
>
> Yes it would.
>
> If the program is not fully known in advance (e.g. the statements are
> being entered in a REPL, or the declaration of 'f' is in a later <script>
> block than the 'y = f;'), then it cannot have been fully "processed for
> function declarations" before the assignment to y, so y will have the
> value undefined (assuming there was no previous definition of 'f'), and
> "this[y]();" will throw without referencing x.
>
> Alternatively, if the program is completely known in advance, then it
> can be fully processed for function declarations, but in that case it is
> statically invalid because f is referenced outside its scope.
>   

I'm sure you've misread the example, considering your confusion with |y
= 'f'|. |this[y]()| effectively calls f().


More information about the Es-discuss mailing list