jam*_*mes 5 javascript design-patterns
我阅读了Javascript设计模式,然后是RMP上的一堆SO答案,我一直发现,在提到缺点的地方,这是本书的直接引用:
这种模式的缺点是,如果私有函数引用公共函数,则如果需要补丁,则不能覆盖该公共函数.这是因为私有函数将继续引用私有实现,并且该模式不适用于公共成员,仅适用于函数.
引用私有变量的公共对象成员也遵循无补丁规则.
因此,使用Revealing Module模式创建的模块可能比使用原始模块模式创建的模块更脆弱,因此在使用期间应该小心.
对不起,我很愚蠢,但上面的解释只是不适合我.有人可以提供一个代码丰富的视觉示例,说明这种劣势意味着什么?
我认为这解释了经常被引用的缺点。就我个人而言,如果您更喜欢组合而不是继承,我认为这没什么大不了的,因为它根本不会出现。
var revealed = (function() {
function foo() {
return baz();
}
function bar() {
return "I am the original bar!";
}
// private function always calls bar because it's in the closure
// and it can't see the "instance" variable
function baz() {
return bar();
}
return { foo : foo, bar : bar }
})();
var child = Object.create(revealed);
child.bar = function() {
return "I am the new bar!";
}
// we want this to call the new bar but it doesn't
console.log(child.foo()); // I am the original bar!
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
76 次 |
| 最近记录: |