Hoisting behaviour of 'const' and 'let'

Mark S. Miller erights at google.com
Sat Oct 11 21:49:39 PDT 2008


On Sat, Oct 11, 2008 at 8:35 PM, David-Sarah Hopwood
<david.hopwood at industrial-designers.co.uk> 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:

The following issue invalidates one of your reasons.

{
    .... f(); ...

    const x = 3;

    function f() { ... x ... }
}

Since functions hoist, if const do not, then in the example above, either
1) we must not allow f to refer to x, or
2) we must still have a read barrier, since f may still be invoked and
access x before x is initialized.

I hope we can agree that #1 is unacceptable. Thus, whether const or
let hoists or not, we must still deal with the same use-before-init
issues.

-- 
    Cheers,
    --MarkM


More information about the Es-discuss mailing list