Hoisting behaviour of 'const' and 'let'

David-Sarah Hopwood david.hopwood at industrial-designers.co.uk
Sun Oct 12 06:29:02 PDT 2008


Yuh-Ruey Chen wrote:
> David-Sarah Hopwood wrote:
>> Mark S. Miller wrote:
>>> On Sat, Oct 11, 2008 at 4:01 PM, Brendan Eich <brendan at mozilla.com> wrote:
>>>>> If we want to avoid the read-barrier, we should not hoist either const
>>>>> or let. If we are to consider not hoisting const, WE NEED TO DECIDE
>>>>> THIS NOW, before ES3.1 mandates a hoisting const.
>>>> A few messages back you nicely repeated the
>>>> least-concepts/least-astonishment/most-symmetric case for hoisting to block
>>>> top that has carried so far. I don't see how we can backtrack here. We'll
>>>> flail hard.
>>>> [...other good stuff snipped...]
>>> I'm convinced; thanks. I agree it's too late to consider non-hoisting
>>> const even if we come to regret it. Given that const hoists, let
>>> declarations must as well, and we can argue about let-read-barriers
>>> and repeated declarations later.
>> I disagree with this reasoning.
>>
>> 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.]

-- 
David-Sarah Hopwood


More information about the Es-discuss mailing list