title: Is using closures to emulate encapsulation a bad idea?
body: <p>For example, take a look at my simple implementation of a stack:</p>
  
  <pre><code>var MyStack = (function() {
      var min;
      var head;
  
      // Constructor
      function MyStack() {
          this.size = 0;
      }
  
      MyStack.prototype.push = function(val) {
          var node = new Node(val);
  
          if (typeof min === 'undefined' || val &lt; min) {
              min = val;
          }
  
          ++this.size;
          if (typeof head === 'undefined') {
              head = node;
          } else {
              node.next = head;
              head = node;
          }
      };
  
      MyStack.prototype.pop = function() {
          if (typeof head === 'undefined') {
              throw new Error('Empty stack');
          }
  
          --this.size;
  
          var data = head.data;
          head = head.next;
  
          return data;
      };
  
      MyStack.prototype.min = function() {
          if (typeof min === 'undefined') {
              throw new Error('Min not defined');
          }
  
          return min;
      };
  
      MyStack.prototype.peek = function() {
          if (typeof head === 'undefined') {
              throw new Error('Empty stack');
          }
  
          return head.data;
      };
  
      function Node(data) {
          this.data = data;
          this.next;
      }
  
      return MyStack;
  })();
  </code></pre>
  
  <p>By using this method, I can make sure that no one is able to (accidentally or intentionally) manipulate "private" fields such as min and head. I can also make use of private functions such as Node() which doesn't need to be exposed.  </p>
  
  <p>I have read that this will use more memory just because of the fact that it has to maintain an additional scope for each new object that is created for MyStack. Does it need so much extra memory that this way is a bad idea?  </p>
  
  <p>I did try to optimize it by making use of prototypes instead of creating functions every time a new object is created. In other words, I didn't include the functions as part of the constructor of MyStack.</p>
  
  <p>My question is, is this poor design? Are there are any major downfalls to this methodology?</p>