The Revealing Module Pattern (揭示模式)

Learning JavaScript Design Patterns

A book by Addy Osmani

最近在读这本书

今日读到揭示模型的时候,对Disadvantages表述产生的疑惑,原文如下:

Advantages

This pattern allows the syntax of our scripts to be more consistent. It also makes it more clear at the end of the module which of our functions and variables may be accessed publicly which eases readability.

Disadvantages

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.

 

在多试尝试以后,我对Disadvantages理解为:

1,这里的can't be overridden的内容应该是包括所有暴露出来的public方法与变量,比如以下原文示例中的start,increment,count;

var myRevealingModule = (function () {
 
        var privateCounter = 0;
 
        function privateFunction() {
            privateCounter++;
        }
 
        function publicFunction() {
            publicIncrement();
        }
 
        function publicIncrement() {
            privateFunction();
        }
 
        function publicGetCount(){
          return privateCounter;
        }
 
        // Reveal public pointers to
        // private functions and properties
 
       return {
            start: publicFunction,
            increment: publicIncrement,
            count: publicGetCount
        };
 
    })();
 
myRevealingModule.start();
View Code

2,can't be overridden是相对于上文中的The Module Pattern说的。

 

------------------------------------------

 验证一:我把原文示例作了以下修改:

    var myRevealingModule = (function () {

        var privateCounter = 0;
        //新增公共变量
        var publicCounter = 1
        var publicObj = {a:"a",b:"b"}

        function privateFunction() {
            privateCounter++;
            console.log("执行privateFunction")
        }

        //新增的函数
        function privateTest() {
            console.log("执行privateTest")
            console.log("publicCounter",publicCounter)
            console.log("publicObj",publicObj)
            publicFunction()
        }

        function publicFunction() {
            console.log("执行start/publicFunction了。。。")
            publicIncrement();
        }

        function publicIncrement() {
            privateFunction();
        }

        function publicGetCount(){
            return privateCounter;
        }

        // Reveal public pointers to
        // private functions and properties

        return {
            publicCtr:publicCounter,  //新增
            publicObj:publicObj,      //新增
            test:privateTest,         //新增
            start: publicFunction,
            increment: publicIncrement,
            count: publicGetCount
        };

    })();
    
    //修改变量与方法
    myRevealingModule.publicCtr = 12
    myRevealingModule.publicObj = {a:"a1",b:"b1"}
    myRevealingModule.start = function () {
        //myRevealingModule.start对应publicFunction
        console.log("执行修改后的start/publicFunction了。。。")
    }
    //执行
    myRevealingModule.test();
View Code

 

得到结果:

 

 由此可见,以上代码中对变量和方法作的修改都没有起作用。
 

 

 

 

验证二:

把原文中the Module Pattern 的示例代码改了一下:

var basketModule = (function () {

        // privates

        var basket = [];

        function doSomethingPrivate() {
            //...
        }

        function doSomethingElsePrivate() {
            //...
        }

        // Return an object exposed to the public
        return {

            // Add items to our basket
            addItem: function( values ) {
                basket.push(values);
            },

            // Get the count of items in the basket
            getItemCount: function () {
                return basket.length;
            },

            // Public alias to a private function
            doSomething: doSomethingPrivate,

            // Get the total value of items in the basket
            getTotal: function () {

                var q = this.getItemCount(),
                    p = 0;

                while (q--) {
                    p += basket[q].price;
                }

                return p;
            }
        };
    })();
    basketModule.addItem=function () {
        console.log("change")
    }
    basketModule.addItem()
View Code

 

得到的结果是:

 

posted @ 2020-12-28 20:50  angular2016  阅读(106)  评论(0)    收藏  举报