任何人都可以確認這些來自Pro Javascript Design Patterns第3章的樣本是否有缺陷,如果是這樣,從根本上來說 - 它們比在JavaScript中產生'class'常量的預期目標還要多打字嗎?謝謝。贊成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:好的部分,而不是你正在閱讀的內容。 – 2011-05-27 04:49:10
@GlennFerrieLive大部分用於記錄,我做過/確實知道原型繼承機制,但是可以看到爲什麼您認爲不然。 – Joffer 2011-11-27 19:29:27