在JavaScript中有使用常量的方法吗?
如果不是,指定用作常量的变量的常见做法是什么?
在JavaScript中有使用常量的方法吗?
如果不是,指定用作常量的变量的常见做法是什么?
当前回答
使用"new"对象api,你可以这样做:
var obj = {};
Object.defineProperty(obj, 'CONSTANT', {
configurable: false
enumerable: true,
writable: false,
value: "your constant value"
});
请在Mozilla MDN上查看更多细节。它不是第一级变量,因为它是附加到对象上的,但如果你有一个作用域,你可以将它附加到那个对象上。这也可以。 例如,在全局作用域中这样做会在窗口上声明一个伪常量值(这是个坏主意,你不应该粗心地声明全局变量)
Object.defineProperty(this, 'constant', {
enumerable: true,
writable: false,
value: 7,
configurable: false
});
> constant
=> 7
> constant = 5
=> 7
注意:赋值会在控制台中返回赋值,但变量的值不会改变
其他回答
从ES2015开始,JavaScript有了const的概念:
const MY_CONSTANT = "some-value";
这将工作在几乎所有的浏览器除了ie8, 9和10。有些可能还需要启用严格模式。
如果你需要支持旧的浏览器或使用遗留代码,你可以使用var和像ALL_CAPS这样的约定来表明某些值不应该被修改:
var MY_CONSTANT = "some-value";
在JavaScript中,我倾向于使用函数返回常量值。
function MY_CONSTANT() {
return "some-value";
}
alert(MY_CONSTANT());
检查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
声明:我是这个工具的作者。
声明一个只读命名的常量。 通过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
如果值得一提的话,你可以使用$ provider .constant()在angular中定义常量
angularApp.constant('YOUR_CONSTANT', 'value');