Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

delete or override const variables in javascript Harmony / ECMAScript 6

Reading and tinkering with the new features offered by ECMAScript 6.

The new 'const' statement for writing constant variables is a nifty feature, which adds features to an already interesting update.

The variable is created as read-only, and once it is stated it can't be overridden.

EDIT: A consequent problem arise, for example, when testing code on the console. Running a script containing a const definition twice, would lead to an error, breaking the execution.

What if I want to release that keyword? Is there any way to unset or delete the variable?

I've read in this post that this is actually a problem which affects the var statement as well, because the environments where the variables are created are different on many level of abstraction.

How ECMAScript 6 intend to address this issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is not possible to redefine variables declared using const.

However, const is block-scoped. To address the issue you describe, when testing some code in the console all you would have to do is wrap your script in { and }:

{ const x = 1; }
{ const x = 2; }

Note that many browsers that already support the const keyword do not yet support block-scoped constants so the example above will fail in Chrome and Firefox (See Kangax's compatibility table for more).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...