What is meant by “public function can't be overridden if a patch is necessary.” in Addy's description of the Revealing Module Pattern?

前端 未结 3 474
南旧
南旧 2020-11-28 12:54

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. T

3条回答
  •  孤街浪徒
    2020-11-28 13:23

    I would bind getName to this, which, it seems, points to the content returned in RMP.

    function makeGreeter(name){
        this.getName = function(){ return name;};
        var _sayHello = function(){console.log("Hello, " + this.getName());};
        return {
                getName: getName,
                sayHello: _sayHello
        }
    }
    

    I prefer this, though:

    function makeGreeter(name){
        this.getName = function(){ return name;};
        var _sayHello = function(){console.log("Hello, " + this.getName());};
        var API = {
            getName: getName,
            sayHello: _sayHello
        };
        return API;
    }
    

提交回复
热议问题