javascript - ES6 why can I reassign a constant when defined in a loop - Stack Overflow

I am playing around with some pointless logic to better understand ES6 and have noticed a strange occur

I am playing around with some pointless logic to better understand ES6 and have noticed a strange occurrence when defining a constant.

It seems possible to change a constant assignment when defined in a loop:

        "use strict";

        for(const i=0;i<10;i++){ //seting constant in loop
            console.log(i); //is reassigned and incremented 0,1,2,3...
        }


        const e = 0; //setting constant outside loop
        for(;e<10;e++){ //cannot reassign constant
            console.log(e);
        }

Is this expected behavior and can anyone shed some light on why this occurs, is declaration in the loop different?


Update from Statements/const

This declaration creates a constant that can be global or local to the function in which it is declared. Constants are block-scoped.

I am playing around with some pointless logic to better understand ES6 and have noticed a strange occurrence when defining a constant.

It seems possible to change a constant assignment when defined in a loop:

        "use strict";

        for(const i=0;i<10;i++){ //seting constant in loop
            console.log(i); //is reassigned and incremented 0,1,2,3...
        }


        const e = 0; //setting constant outside loop
        for(;e<10;e++){ //cannot reassign constant
            console.log(e);
        }

Is this expected behavior and can anyone shed some light on why this occurs, is declaration in the loop different?


Update from Statements/const

This declaration creates a constant that can be global or local to the function in which it is declared. Constants are block-scoped.

Share Improve this question edited Feb 5, 2015 at 14:48 Simon Staton asked Feb 5, 2015 at 12:46 Simon StatonSimon Staton 4,5054 gold badges31 silver badges50 bronze badges 4
  • 1 Wow, just tried this on Chrome Version 40.0.2214.94 and it generates an infinite loop, maybe because the constant is never muted. Which browser are you using? – Sarcadass Commented Feb 5, 2015 at 12:51
  • That problably means chrome has it correct by not reassigning it(? but should probably throw an error) and still hoists the constant. In your case it seems like the const acts like let developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – René Commented Feb 5, 2015 at 13:08
  • In Firefox I get TypeError: redeclaration of var i for the first example – kapex Commented Feb 5, 2015 at 13:14
  • @BenjaminBlonde I am using canary v42, if you include "use strict"; it will not create the infinite loop and will increment – Simon Staton Commented Feb 5, 2015 at 13:53
Add a ment  | 

1 Answer 1

Reset to default 8

When you modify an "immutable binding", the current draft only throws in the strict mode:

As @kangax pointed out, reassignment of a constant should always throw, since const creates an "immutable binding" with the strict flag on (here):

If IsConstantDeclaration of d is true, then

Call env’s CreateImmutableBinding concrete method passing dn and true as the arguments.

and then:

SetMutableBinding (N,V,S) ...

  1. Else if the binding for N in envRec is a mutable binding, change its bound value to V.
  2. Else this must be an attempt to change the value of an immutable binding so if S is true throw a TypeError exception.

However, node only throws in strict mode:

"use strict";

const e = 0;
e = 42;  // SyntaxError: Assignment to constant variable.

(it's not clear why this is a "SyntaxError")...

In the non-strict mode, the assignment to the constant is silently ignored:

const e = 0;
e = 42;
console.log(e); // 0

Tested with node v0.10.35 with --harmony flag.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745468710a4629033.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信