Hoisting behaviour of 'const' and 'let'

David-Sarah Hopwood david.hopwood at industrial-designers.co.uk
Mon Oct 13 15:49:48 PDT 2008


Brendan Eich wrote:
> On Oct 12, 2008, at 1:45 PM, David-Sarah Hopwood wrote:
>> Peter Michaux wrote:
>>> On Sun, Oct 12, 2008 at 12:11 PM, David-Sarah Hopwood
>>> <david.hopwood at industrial-designers.co.uk> wrote:
>>>> Peter Michaux wrote:
>>>>> David-Sarah wrote:
>>>>>>
>>>>>> (function () {
>>>>>> print(x);
>>>>>> { const x = 42; }
>>>>>> })();
>>>>>>
>>>>>> (calls 'print(undefined)').
>>>
> . . .
>>> It would be completely unintelligible to read code where the print
>>> above shows "42".
>>
>> Of course. In both my proposal and in the current draft of ES3.1, this
>> is a static error, because x isn't in scope at the 'print' -- except
>> that if there is an x declared in some surrounding scope, that x is used.
> 
> No, not in ES3.1 -- quoting from the latest (draft13oct08), section 12.15:
> 
> [...] Any attempts to access the value of a constant before it is
> assigned a value throws a ReferenceError exception.

OK, "statically detectable error", then.

-- 
David-Sarah Hopwood


More information about the Es-discuss mailing list