title: JavaScript private methods
body: <p>To make a JavaScript class with a public method I'd do something like:</p>
  
  <pre><code>function Restaurant() {}
  
  Restaurant.prototype.buy_food = function(){
     // something here
  }
  
  Restaurant.prototype.use_restroom = function(){
     // something here
  }
  </code></pre>
  
  <p>That way users of my class can:</p>
  
  <pre><code>var restaurant = new Restaurant();
  restaurant.buy_food();
  restaurant.use_restroom();
  </code></pre>
  
  <p>How do I create a private method that can be called by the <code>buy_food</code> and <code>use_restroom</code> methods but not externally by users of the class?</p>
  
  <p>In other words, I want my method implementation to be able to do:</p>
  
  <pre><code>Restaurant.prototype.use_restroom = function() {
     this.private_stuff();
  }
  </code></pre>
  
  <p>But this shouldn't work:</p>
  
  <pre><code>var r = new Restaurant();
  r.private_stuff();
  </code></pre>
  
  <p>How do I define <code>private_stuff</code> as a private method so both of these hold true?</p>
  
  <p>I've read <a href="http://javascript.crockford.com/private.html" rel="noreferrer">Doug Crockford's writeup</a> a few times but it doesn't seem like "private" methods can be called by public methods and "privileged" methods can be called externally.</p>