【发布时间】:2016-12-10 10:48:17
【问题描述】:
从 ES6 开始,我们有 const。
这是不允许的:
const x; //declare first
//and then initialize it
if(condition) x = 5;
else x = 10;
这是有道理的,因为它会阻止我们在初始化之前使用常量。
如果我这样做了
if(condition)
const x = 5;
else
const x = 10;
x 变为块作用域。
那么如何有条件地创建一个常量呢?
【问题讨论】:
-
这是我在 Javascript 中错过的东西,Java 做得很好
标签: javascript ecmascript-6 constants