title: How do I call a public function from within a private function in the JavaScript Module Pattern
body: <p><strong>How do I call a public function from within a private function in the JavaScript Module Pattern?</strong></p>
  
  <p>For example, in the following code,</p>
  
  <pre><code>var myModule = (function() {
      var private1 = function(){
          // How to call public1() here?
          // this.public1() won't work
      }
  
      return {
          public1: function(){ /* do something */}
      }
  })();
  </code></pre>
  
  <p>This question has been asked <a href="https://stackoverflow.com/questions/1111725/javascript-calling-public-method-from-private-one-within-same-object">twice</a> <a href="https://stackoverflow.com/questions/4505073/how-can-i-call-a-public-method-from-within-a-private-one-when-using-the-javascri?lq=1">before</a>, with a different accepted answer for each.</p>
  
  <ol>
  <li>Save a reference to the return object before returning it, and then use that reference to access the public method. See <a href="https://stackoverflow.com/a/1111752/1171180">answer</a>.</li>
  <li>Save a reference to the public method in the closure, and use that to access the public method. See <a href="https://stackoverflow.com/a/4505114/1171180">answer</a>.</li>
  </ol>
  
  <p>While these solutions work, they are unsatisfactory from an OOP point of view. To illustrate what I mean, let's take a concrete implementation of a snowman with each of these solutions and compare them with a simple object literal.</p>
  
  <p>Snowman 1: Save reference to return object</p>
  
  <pre><code>var snowman1 = (function(){
    var _sayHello = function(){
      console.log("Hello, my name is " + public.name());
    };
  
    var public = {
      name: function(){ return "Olaf"},
      greet: function(){
        _sayHello();
      }
    };
    return public;
  })()
  </code></pre>
  
  <p>Snowman 2: Save reference to public function</p>
  
  <pre><code>var snowman2 = (function(){
    var _sayHello = function(){
      console.log("Hello, my name is " + name());
    };
    var name = function(){ return "Olaf"};
  
    var public = {
      name: name,
      greet: function(){
        _sayHello();
      }
    };
    return public;
  })()
  </code></pre>
  
  <p>Snowman 3: object literal</p>
  
  <pre><code>var snowman3 = {
      name: function(){ return "Olaf"},
      greet: function(){
        console.log("Hello, my name is " + this.name());
      }
  }
  </code></pre>
  
  <p>We can see that the three are identical in functionality and have the exact same public methods.</p>
  
  <p>If we run a test of simple overriding, however</p>
  
  <pre><code>var snowman = // snowman1, snowman2, or snowman3
  snowman.name = function(){ return "Frosty";}
  snowman.greet(); // Expecting "Hello, my name is Frosty"
                   // but snowman2 says "Hello, my name is Olaf"
  </code></pre>
  
  <p>we see that #2 fails.</p>
  
  <p>If we run a test of prototype overriding, </p>
  
  <pre><code>var snowman = {};
  snowman.__proto__ = // snowman1, snowman2, or snowman3
  snowman.name = function(){ return "Frosty";}
  snowman.greet(); // Expecting "Hello, my name is Frosty"
                   // but #1 and #2 both reply "Hello, my name is Olaf"
  </code></pre>
  
  <p>we see that both #1 and #2 fail. </p>
  
  <p>This is a really ugly situation. Just because I've chosen to refactor my code in one way or another, the user of the returned object has to look carefully at how I've implemented everything to figure out if he/she can override my object's methods and expect it to work! While opinions differ here, my own opinion is that the correct override behavior is that of the simple object literal.</p>
  
  <p>So, this is the real question:</p>
  
  <p><strong>Is there a way to call a public method from a private one so that the resulting object acts like an object literal with respect to override behavior?</strong></p>