Clarification on let/const

Nicholas C. Zakas standards at nczconsulting.com
Tue Jul 31 11:00:34 PDT 2012


Hi,

I've been going through the spec trying to figure out a few edge cases 
for block bindings. It appears that there is not a functioning demo for 
these (Traceur is mostly working with var semantics, Firefox/Chrome are 
not at all spec-compliant), so just thought I would ask to get a better 
understanding of the expected behavior.

1. If I'm interpreting this correctly, let and const declarations will 
throw an error if used with an identifier that is already defined within 
the same block. So I think that means this should throw an error:

    function f() {
         var count = 10;
         let count = 20;   // error?
    }


However, it seems like this would not (block variable shadowing function 
variable?):

    function f() {
         var count = 10;

         if (condition) {
             let count = 20;   // no error?
         }
    }


And I would assume this is the same (using let instead of var):

    function f() {
         let count = 10;

         if (condition) {
             let count = 20;   // no error?
         }
    }

Am I correct in my understanding of these?

2. Since ECMAScript 6 is always running in strict mode, I'm assuming 
that attempting to assign to a declared constant would throw an error 
(in that it behaves similar to a non-writable property in strict mode)?

    const MAX_ITEMS = 30;

    MAX_ITEMS = 25;    // error?


Is that correct?


Thanks,
Nicholas

-- 
___________________________
Nicholas C. Zakas
http://www.nczonline.net

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120731/881beedd/attachment.html>


More information about the es-discuss mailing list