Jof*_*fer 3 javascript design-patterns errata
任何人都可以确认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');
Run Code Online (Sandbox Code Playgroud)
小智 7
我想,这是错的.如前所述,"this"指的是窗口对象,代码也有语法错误.以下代码应该完成所需的目标:
var Class = (function () {
// Private static attributes.
var constants = {
UPPER_BOUND: 100,
LOWER_BOUND: -100
};
var sc = function (constructorArgument) {
};
// Privileged static method.
sc.getConstant = function (name) {
return constants[name];
};
// Return the constructor.
return sc;
})();
alert(Class.getConstant('UPPER_BOUND'));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
749 次 |
| 最近记录: |