我一直在阅读 revealing module pattern
在 Addy Osmani 的书中。它突出了以下缺点:
A disadvantage of this pattern is that if a private function refers to a public function, that public function can't be overridden if a patch is necessary. This is because the private function will continue to refer to the private implementation and the pattern doesn't apply to public members, only to functions.
Public object members which refer to private variables are also subject to the no-patch rule notes above.
As a result of this, modules created with the Revealing Module pattern may be more fragile than those created with the original Module pattern, so care should be taken during usage.
我不是 JS 方面的专家,这对我来说没有多大意义。另外书不提供任何与模块模式相比,这些缺点的示例 .
我用谷歌搜索并在这里找到了这个:
Revealing module pattern disadvantages
这又是我无法掌握的。使用的示例是使用构造函数。
有人可以请ELI5吗?
最佳答案
这可以通过下面的代码来理解。
var myRevealingModule= (function(){
let privateVar = 'Kirti';
let publicVar ='Hye there';
function getName () {
console.log('name is ' + privateVar);
}
function privateGetNames(){
getName()
sayGreeting();
}
function sayGreeting() {
console.log(publicVar);
}
return {
getname: privateGetNames,
sayGreeting: sayGreeting
}
})();
//earlier result
myRevealingModule.getname();
//overriding a public method
myRevealingModule.sayGreeting = () => console.log('helloooo change');
myRevealingModule.getname();
在这个 myRevealingModule 有一个私有(private)方法 私有(private)获取姓名 访问说问候 一个公共(public)方法,现在如果我想稍后覆盖 sayGreeting(就像我上面所做的那样),privateGetName 将继续访问它的私有(private) sayGreeting 而不是被覆盖的 说问候 我们稍后宣布。
因此很难修补这些公共(public)方法
有一篇非常好的文章可以帮助您进行详细的解释。 https://developerslogblog.wordpress.com/2017/10/22/drawbacks-in-the-javascript-module-pattern/
关于Javascript揭示模块模式的缺点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31221668/