Hoisting behaviour of 'const' and 'let'

David-Sarah Hopwood david.hopwood at industrial-designers.co.uk
Sun Oct 12 15:27:27 PDT 2008


Yuh-Ruey Chen wrote:
> 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.

Oh, right. But it doesn't matter at all, the argument I gave still
applies with minor corrections. I'll write it out again below with
those corrections.

>>> 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 "this[y]();"), then it cannot have been fully
"processed for function declarations" before the access of this['f'],
so this['f'] will have the value undefined (assuming there was no
previous definition of 'f' in the global scope), 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.

-- 
David-Sarah Hopwood


More information about the Es-discuss mailing list