title: Javascript "private" vs. instance properties
body: <p>I'm doing some Javascript R&amp;D and, while I've read <strong>Javascript: The Definitive Guide</strong> and <strong>Javascript Object Oriented Programming</strong>, I'm still having minor issues getting my head out of class based OOP and into lexical, object based OOP.  </p>
  
  <p>I love modules.  Namespaces, subclasses and interfaces. w00t.  Here's what I'm playing with:</p>
  
  <pre><code>var Classes = {
      _proto : {
          whatAreYou : function(){
              return this.name;
          }
      },
      Globe : function(){
          this.name = "Globe"
      },
      Umbrella : new function(){
          this.name = "Umbrella"
      }(),
      Igloo : function(){
          function Igloo(madeOf){
              this.name = "Igloo"
              _material = madeOf;
          }
          // Igloo specific
          Igloo.prototype = {
              getMaterial : function(){
                  return _material;
              }
          }
          // the rest
          for(var p in Classes._proto){
              Igloo.prototype[p] = Classes._proto[p]
          }
          return new Igloo(arguments[0]);
      },
      House : function(){
          function House(){
              this.name = "My House"
          }
          House.prototype = Classes._proto
          return new House()
      }
  }
  Classes.Globe.prototype = Classes._proto
  Classes.Umbrella.prototype = Classes._proto
  
  $(document).ready(function(){    
      var globe, umb, igloo, house;
  
      globe     = new Classes.Globe();
      umb       = Classes.Umbrella;
      igloo     = new Classes.Igloo("Ice");
      house     = new Classes.House();
  
      var objects = [globe, umb, igloo, house]
  
      for(var i = 0, len = objects.length; i &lt; len; i++){
          var me = objects[i];
          if("whatAreYou" in me){
              console.log(me.whatAreYou())
          }else{
              console.warn("unavailable")
          }
      }
  })
  </code></pre>
  
  <p>Im trying to find the best way to modularize my code (and understand prototyping) and separate everything out.  Notice <code>Globe</code> is a function that needs to be instantiated with <code>new</code>, <code>Umbrella</code> is a singleton and already declared, <code>Igloo</code> uses something I thought about at work today, and seems to be working as well as I'd hoped, and <code>House</code> is another Iglooesque function for testing.</p>
  
  <p>The output of this is:</p>
  
  <pre><code>Globe
  unavailable
  Igloo
  My House
  </code></pre>
  
  <p>So far so good. The Globe prototype has to be declared outside the Classes object for syntax reasons, Umbrella can't accept due to it already existing (or instantiated or... dunno the "right" term for this one), and Igloo has some closure that declares it for you.</p>
  
  <p>HOWEVER...</p>
  
  <p>If I were to change it to:</p>
  
  <pre><code>var Classes = {
      _proto : {
          whatAreYou : function(){
              return _name;
          }
      },
      Globe : function(){
          _name = "Globe"
      },
      Umbrella : new function(){
          _name = "Umbrella"
      }(),
      Igloo : function(){
          function Igloo(madeOf){
              _name = "Igloo"
              _material = madeOf;
          }
          // Igloo specific
          Igloo.prototype = {
              getMaterial : function(){
                  return _material;
              }
          }
          // the rest
          for(var p in Classes._proto){
              Igloo.prototype[p] = Classes._proto[p]
          }
          return new Igloo(arguments[0]);
      },
      House : function(){
          function House(){
              _name = "My House"
          }
          House.prototype = Classes._proto
          return new House()
      }
  }
  Classes.Globe.prototype = Classes._proto
  Classes.Umbrella.prototype = Classes._proto
  
  $(document).ready(function(){    
      var globe, umb, igloo, house;
  
      globe     = new Classes.Globe();
      umb       = Classes.Umbrella;
      igloo     = new Classes.Igloo("Ice");
      house     = new Classes.House();
  
      var objects = [globe, umb, igloo, house]
  
      for(var i = 0, len = objects.length; i &lt; len; i++){
          var me = objects[i];
          if("whatAreYou" in me){
              console.log(me.whatAreYou())
          }else{
              console.warn("unavailable")
          }
      }
  })
  </code></pre>
  
  <p><strong>and make <code>this.name</code> into <code>_name</code></strong> (the "private" property), it doesn't work, and instead outputs:</p>
  
  <pre><code>My House
  unavailable
  My House
  My House
  </code></pre>
  
  <p>Would someone be kind enough to explain this one?  Obviously <code>_name</code> is being overwritted upon each iteration and not reading the object's property of which it's attached.  </p>
  
  <p>This all seems a little too verbose needing <code>this</code> and kinda weird IMO. </p>
  
  <p>Thanks :)</p>