【发布时间】:2011-05-26 02:14:37
【问题描述】:
谁能确认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');
【问题讨论】:
-
Javascript 不是经典的。学习原型或使用另一种语言?阅读 JS:好的部分,而不是你正在阅读的内容。
-
@GlennFerrieLive 主要是为了记录,我确实/确实知道原型继承机制,但可以看到你为什么不这样认为
标签: javascript design-patterns errata