简体   繁体   English

Javascript 揭示模块模式的缺点

[英]Javascript revealing module pattern disadvantages

I have been reading about revealing module pattern in Addy Osmani's book.我一直在阅读 Addy Osmani 的书中关于revealing module pattern的内容。 It highlights following disadvantage:它突出了以下缺点:

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.因此,使用揭示模块模式创建的模块可能比使用原始模块模式创建的模块更脆弱,因此在使用过程中应小心。

I'm not an expert in JS and it is not making much sense to me.我不是 JS 专家,这对我来说没有多大意义。 Also book doesn't provide any examples of these disadvantage comparing to module pattern .此外,与模块模式相比,本书没有提供这些缺点的任何示例

I googled and found this on here:我用谷歌搜索并在这里找到了这个:

Revealing module pattern disadvantages 揭示模块模式的缺点

which again I'm not able to grasp.这又是我无法理解的。 As examples being used are using constructor functions.正在使用的示例是使用构造函数。

Could someone please ELI5 please.有人可以请 ELI5。

This could be understood by the code below.这可以通过下面的代码来理解。

    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();

in this myRevealingModule has a private method privateGetName which access sayGreeting a public Method and now if I want to override sayGreeting later (like I did above) the privateGetName will continue to access its private sayGreeting not the overridden sayGreeting that we declared later.在这个myRevealingModule 中有一个私有方法privateGetName ,它访问sayGreeting一个公共方法,现在如果我想稍后覆盖 sayGreeting(就像我上面所做的那样),privateGetName 将继续访问它的私有 sayGreeting 而不是我们稍后声明的覆盖的sayGreeting

hence its very difficult to patch these public methods因此很难修补这些公共方法

There is a very nice article which can help you on with detailed explaination.有一篇非常好的文章可以帮助您进行详细的解释。 https://developerslogblog.wordpress.com/2017/10/22/drawbacks-in-the-javascript-module-pattern/ https://developerslogblog.wordpress.com/2017/10/22/drawbacks-in-the-javascript-module-pattern/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM