const initializers: once more unto the breach

Andy Wingo wingo at
Tue Mar 20 08:45:54 PDT 2012


Unifying the specifications of let and const in the latest draft means
that now, const statements can have no initializer.  Is that intended?

Beyond that, const is just a bit strange.  For example:

  (function (){
     baz = 20;
     return baz;
     const baz = 30;
     baz = 40;
     baz = 50;
   => 20


  (function (){
     const baz;
     return function(x) { baz = x; return function() { return baz; } };
   => 20

I don't really get the point of const, to be honest.  It doesn't help
implementors, because the compiler already knows if a variable is
assigned more than once.

It doesn't seem to help programmers, because the delayed initialization
with temporal dead zone means that you might have to do just as much
mental work to figure out what sets a const var as before.  Seeing
"const baz;" is pretty strange.

Ignorant suggestion: either require initializers, and disallow
assignment to const variables outside an initializer, or remove const
from the language.



More information about the es-discuss mailing list