在JavaScript中有使用常量的方法吗?

如果不是,指定用作常量的变量的常见做法是什么?


当前回答

检查https://www.npmjs.com/package/constjs,它提供了三个函数来创建枚举、字符串const和位图。返回的结果要么是冻结的,要么是密封的,因此你不能在属性创建后更改/删除,你也不能向返回的结果添加新的属性

创建枚举:

var ConstJs = require('constjs');

var Colors = ConstJs.enum("blue red");

var myColor = Colors.blue;

console.log(myColor.isBlue()); // output true 
console.log(myColor.is('blue')); // output true 
console.log(myColor.is('BLUE')); // output true 
console.log(myColor.is(0)); // output true 
console.log(myColor.is(Colors.blue)); // output true 

console.log(myColor.isRed()); // output false 
console.log(myColor.is('red')); // output false 

console.log(myColor._id); // output blue 
console.log(myColor.name()); // output blue 
console.log(myColor.toString()); // output blue 

// See how CamelCase is used to generate the isXxx() functions 
var AppMode = ConstJs.enum('SIGN_UP, LOG_IN, FORGOT_PASSWORD');
var curMode = AppMode.LOG_IN;

console.log(curMode.isLogIn()); // output true 
console.log(curMode.isSignUp()); // output false 
console.log(curMode.isForgotPassword()); // output false 

创建String const:

var ConstJs = require('constjs');

var Weekdays = ConstJs.const("Mon, Tue, Wed");
console.log(Weekdays); // output {Mon: 'Mon', Tue: 'Tue', Wed: 'Wed'} 

var today = Weekdays.Wed;
console.log(today); // output: 'Wed'; 

创建位图:

var ConstJs = require('constjs');

var ColorFlags = ConstJs.bitmap("blue red");
console.log(ColorFlags.blue); // output false 

var StyleFlags = ConstJs.bitmap(true, "rustic model minimalist");
console.log(StyleFlags.rustic); // output true 

var CityFlags = ConstJs.bitmap({Chengdu: true, Sydney: false});
console.log(CityFlags.Chengdu); //output true 
console.log(CityFlags.Sydney); // output false 

var DayFlags = ConstJs.bitmap(true, {Mon: false, Tue: true});
console.log(DayFlags.Mon); // output false. Default val wont override specified val if the type is boolean  

欲了解更多信息,请结帐

https://www.npmjs.com/package/constjs 或https://github.com/greenlaw110/constjs

声明:我是这个工具的作者。

其他回答

忘记IE,使用const关键字。

有一段时间,我在传递给with()语句的对象字面量中指定了“常量”(实际上仍然不是常量)。我觉得这很聪明。这里有一个例子:

with ({
    MY_CONST : 'some really important value'
}) {
    alert(MY_CONST);
}

在过去,我还创建了一个CONST名称空间,将所有常量放在其中。还有开销。天哪。

现在,我写var MY_CONST = 'whatever';吻。

声明一个只读命名的常量。 通过const声明的变量不能被重新声明或重新赋值。 常量可以用大写或小写来声明,但通用的 惯例是全部使用大写字母。

// const c;
// c = 9;   //intialization and declearation at same place
const c = 9;
// const c = 9;// re-declare and initialization is not possible
console.log(c);//9

我在Greasemonkey脚本中使用const而不是var,但这是因为它们只会在Firefox上运行… 名称约定也确实是可行的方法(我两者都做!)

Mozillas MDN Web Docs包含了关于const的很好的例子和解释。摘录:

// define MY_FAV as a constant and give it the value 7
const MY_FAV = 7;

// this will throw an error - Uncaught TypeError: Assignment to constant variable.
MY_FAV = 20;

但遗憾的是,IE9/10仍然不支持const。荒谬的原因是:

IE9用const做什么?所以 到目前为止,我们的决定是不去 支持它。这还不是一个共识 功能,因为它从来没有可用 在所有浏览器上。 ... 最后,这似乎是最好的 网络的长期解决方案是 把它放在外面等着 标准化流程来运行它们 课程。

他们没有实现它是因为其他浏览器没有正确地实现它?!太害怕做得更好?标准定义与否,常量就是常量:设置一次,永不改变。

所有的想法:每个函数都可以被重写(XSS等)。所以var和function(){return}没有区别。Const是唯一真正的常量。

更新: IE11支持const:

IE11支持新兴的ECMAScript 6标准中定义良好且常用的特性,包括let、const、Map、Set和WeakMap,以及__proto__,以改进互操作性。