虽然许多编程语言提供了const关键字来支持常量的声明,但JavaScript里没有表示常量的语义。我们可以用全大写的方式来声明变量,表明它实际上是个常量:
Math.PI; // 3.141592653589793 Math.SQRT2; // 1.4142135623730951 Number.MAX_VALUE; // 1.7976931348623157e+308
通常这种常量会用对象来包装起来:比如上面的Math。要实现程序里需要的常量,可以这样做:
// constructor
var Widget = function () {
// implementation...
};
// constants
Widget.MAX_HEIGHT = 320;
Widget.MAX_WIDTH = 480;
同样的思路,可以使通过字面声明方式创建的对象也可以有全大写的属性。但上面的实现并不能阻止用户改变“常量”(实际上是变量)的值。为了解决这一问题,可以声明一个专门保存常量的对象,比如叫consot,并通过下面的3个方法来统一提供常量的定义与值的获取:
- set(name, value) 定义一个名为name的常量,值为value;
- isDefined(name) 检查一个名为name的常量有没有被定义;
- get(name) 得到名为name的常量的值
在下面的const实现中,只支持简单类型的常量数据。通过hasOwnProperty() 来检查数据类型是否可用,以及常量是否已经被定义:
var constant = (function () {
var constants = {},
ownProp = Object.prototype.hasOwnProperty,
allowed = {
string: 1,
number: 1,
boolean: 1
},
prefix = (Math.random() + "_").slice(2);
return {
set: function (name, value) {
if (this.isDefined(name)) {
return false;
}
if (!ownProp.call(allowed, typeof value)) {
return false;
}
constants[prefix + name] = value;
return true;
},
isDefined: function (name) {
return ownProp.call(constants, prefix + name);
},
get: function (name) {
if (this.isDefined(name)) {
return constants[prefix + name];
}
return null;
}
};
}());
检查这个实现:
// check if defined
constant.isDefined("maxwidth"); // false
// define
constant.set("maxwidth", 480); // true
// check again
constant.isDefined("maxwidth"); // true
// attempt to redefine
constant.set("maxwidth", 320); // false
// is the value still intact?
constant.get("maxwidth"); // 480