Hoisting behaviour of 'const' and 'let'

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


David-Sarah Hopwood wrote:
> Yuh-Ruey Chen wrote:
>> David-Sarah Hopwood wrote:
>>> The reason for making 'const' hoist to the top of the enclosing block,
>>> AFAIR, was consistency with function declarations. However, there are
>>> good reasons why 'const'/'let' and function declarations should be
>>> treated differently:
>>>
>>> <snip>
>> I think you're forgetting about |var| hoisting.
> 
> I'm not forgetting, but I don't think it's relevant, because hoisting was
> exactly the problem with 'var' that we're trying to fix. That is,
> 
>  - the hoisting behaviour of 'let' *must* differ from 'var', otherwise
>    what is the point of 'let'?
>  - the hoisting behaviour of 'const' should be the same as 'let',
>    because:
>     * it doesn't need to hoist for backward compatibility, unlike 'var';
>     * the argument about guaranteed initialization is even more important
>       for 'const' than it is for 'let';
>     * obviously we do not want four different hoisting behaviours
>       (for 'var', 'function', 'const' and 'let'). [Note that ES3
>       already has two hoisting behaviours, for 'var' and 'function',
>       so what I've proposed would only be adding one more.]

      * if 'const' has the same hoisting behaviour as 'let', then we
        don't need a separate 'let const'.

-- 
David-Sarah Hopwood


More information about the Es-discuss mailing list