Javascript揭示模块模式的缺点 [英] Javascript revealing module pattern disadvantages

查看:107
本文介绍了Javascript揭示模块模式的缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读Addy Osmani的书中的显示模块模式。它突出显示了以下缺点:

I have been reading about revealing module pattern in Addy Osmani's book. It highlights following disadvantage:


此模式的缺点是,如果私有功能引用
a公共功能,则该公共功能如果需要补丁
,则不能覆盖它。这是因为私有功能将继续
引用私有实现,并且该模式不适用于
公共成员,仅适用于函数。

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方面的专家,对我来说没有太大意义。另外,本书没有提供与模块模式相比这些缺点的任何 示例。

I'm not an expert in JS and it is not making much sense to me. Also book doesn't provide any examples of these disadvantage comparing to module pattern.

我用Google搜索后发现了这一点在此处:

I googled and found this on here:

显示模块模式的缺点

这又是我无法掌握的。作为示例,使用构造函数。

which again I'm not able to grasp. As examples being used are using constructor functions.

有人可以请ELI5吗?

Could someone please ELI5 please.

推荐答案

下面的代码可以理解这一点。

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

在此 myRevealingModule 中具有私有方法 privateGetName 哪个访问 sayGreeting 一个公共方法,现在,如果我以后想要覆盖sayGreeting(就像我在上面所做的那样),privateGetName将继续访问其私有的sayGreeting,而不是被覆盖的 sayGreeting

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.

因此很难修补这些公共方法

hence its very difficult to patch these public methods

有一篇非常不错的文章可以帮助您进行详细说明。 https://developerslogblog.wordpress.com / 2017/10/22 / drawbacks-in-the-javascript-module-pattern /

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/

这篇关于Javascript揭示模块模式的缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆