Pro Javascript 设计模式勘误表?
任何人都可以确认《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');
Can anyone confirm that these samples from Chapter 3 of Pro Javascript Design Patterns are flawed and, if so, how fundamentally - are they more than a typo or two away from producing the intended goal of 'class' constants in JavaScript? Thanks.
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');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为,这是错误的。正如前面提到的,“this”指的是窗口对象,代码也存在语法错误。以下代码应该实现所需的目标:
I think, this is wrong. As mentioned previously, "this" refers to the window object and the code also has a syntax error. The following code should accomplish the required goal:
该代码可以简单地修复,因为
代码的其余部分是过度设计的或完全错误的,应该被忽略。
如果您关心只读,请将可写标志设置为 false(注意默认值为 false)。
The code can be trivially fixed as
The rest of the code is over engineered or plain wrong and should be ignored.
If you care about being read only then set the writable flag to false (note the default is false).
警惕任何声称是“Pro”的东西。我没有读过这本书,但我对代码的看法如下:
“属性”这个词是错误的,它应该是“属性”或“变量”,因为它们是变量,也可以描述为属性本地激活/变量对象。
该代码将在全局上下文中执行,其中
this
是窗口/全局对象。因此,如果存在全局 *getUPPER_BOUND* 函数,则将不带参数调用该函数。它后面是一个大括号 ({),它在不能出现块的位置打开一个块,因此这是一个语法错误。我假设以下内容是有意的:
它创建全局/窗口对象的 getUPPER_BOUND 属性,该属性在代码运行时分配 RHS 上的匿名函数。
这是分配给全局变量“Class”的函数。
经过修复,它可能会“工作”,但并不优雅。任何在代码中出现如此明显错误的书都没有经过仔细编写,并且在出版之前肯定也没有经过适当的审查。
使用信誉良好的在线资源,并继续就您不理解或认为有错误的任何问题提出问题。还有其他讨论 javascript 的论坛可以为技术问题提供更详细的答案。
Be wary of anything claiming to be "Pro" whatever. I haven't read the book, but my take on the code is as follows:
The word "attributes" is wrong, it should be either "properties" or "variables" because they are variables, which can also be described as properties of the local activation/variable object.
The code will be executed in a global context where
this
is the window/global object. So if there is a global *getUPPER_BOUND* function, it will be called with no arguments. It is followed by a curly brace ({) which opens a block in a place where a block can't be, so that is a syntax error.I presume the following was intended:
which creates a getUPPER_BOUND property of the global/window object that is assiged the anonymous function on the RHS when the code is run.
This is the function that is assigned to the global variable "Class".
With fixes it may "work", but not elegantly. Any book with such glaring errors in the code has not been carefully written and certainly hasn't been properly reviewed before being published.
Use reputable online resources and continue to ask questions about anything you don't understand or think is in error. There are other forums for discussing javascript that can provide far more detailed answers for technical questions.
看看这个作为一个不错的选择: http://www.klauskomenda.com/code /javascript-programming-patterns/
对于不可变的公共属性,正如建议的那样,使用 Object.freeze 和 John Resig 的好建议:http://ejohn.org/blog/ecmascript-5-objects-and-properties /
并且为了不破坏全局范围,请将命名空间添加到 jQuery:是否可以在 jQuery 中创建命名空间?
Check this out as a good alternative: http://www.klauskomenda.com/code/javascript-programming-patterns/
and for immutable public properties, as was suggested, use Object.freeze and John Resig's great advice: http://ejohn.org/blog/ecmascript-5-objects-and-properties/
and for a way to not clobber your global scope, add namespaces to jQuery: Is it possible to create a namespace in jQuery?
让它工作,但不确定这是否是作者的意图。
Got this to work, but not sure if this was what the authors intended it to be.
这样做怎么样?
How about do it this way?