title: General setter/getter methods for private properties
body: <p>I can create private members on a JavaScript class like this:</p>
  
  <pre><code>function Class(_foo, _bar) {
      var foo = _foo, // private, yay
          bar = _bar; // also private, also yay
  }
  </code></pre>
  
  <p>How can I implement general setter/getter methods for this class? I suppose one way to do it would be to use <code>eval</code>:</p>
  
  <pre><code>function Class(_foo, _bar) {
      var foo = _foo,
          bar = _bar;
  
      this.get = function(property) {
          return eval(property);
      };
  
      this.set = function(property, value) {
          eval(property + " = " + value);
      };
  }
  </code></pre>
  
  <p>However, this solution is ugly. Another way to do it is to create a <code>private</code> object and then access properties on that:</p>
  
  <pre><code>function Class(foo, bar) {
      var private = {
          foo: foo,
          bar: bar 
      };
  
      this.get = function(property) {
          return private[property];
      };
  
      this.set = function(property, value) {
          private[property] = value;
      };
  }
  </code></pre>
  
  <p>I don't like this solution very much either, though, because it means I have to access the <code>private</code> object all over the place. Surely there's a cleaner way to provide a general getter/setter for private properties on a JavaScript class? </p>
  
  <p>I realize similar questions have already been asked, but it doesn't appear that any of them ask this specific question; that is, how to provide a single getter/setter for all of the private properties on the class.</p>
  
  <p>Here's a <a href="http://jsfiddle.net/TbhF4/2/" rel="nofollow">jsFiddle</a> with the two examples.</p>