任何人都可以确认这些来自Pro Javascript 设计模式第 3 章的示例存在缺陷吗?如果是这样,从根本上来说,它们是否与在 JavaScript 中生成“类”常量的预期目标相差不止一个或两个?谢谢。
var Class = (function() {
// Constants (created as private static attributes).
var UPPER_BOUND = 100;
// Privileged static method.
this.getUPPER_BOUND() {//sic
return UPPER_BOUND;
}
...
// Return the constructor.
return function(constructorArgument) {
...
}
})();
/* Usage. */
Class.getUPPER_BOUND();
/* Grouping constants together. */
var Class = (function() {
// Private static attributes.
var constants = {
UPPER_BOUND: 100,
LOWER_BOUND: -100
}
// Privileged static method.
this.getConstant(name) {//sic
return constants[name];
}
...
// Return the constructor.
return function(constructorArgument) {
...
}
})();
/* Usage. */
Class.getConstant('UPPER_BOUND');