title: Preferred way of encapsulating Javascript and defining private variables/methods?
body: <p>So before I always used this method of encapsulating my Javascript:</p>
  
  <pre><code>Classtype = (function (){
  //private members
  
      var variable1;
      var variable2;
  
  //
  
  //public methods
      return {
          SetVariable1: function(pvariable){
          variable1 = pvariable;
          },
  
          GetVariable1: function(){
          return variable1;
          }
  
      };    
  })();
  
  $(document).ready(function(){
  
      Classtype.SetVariable1('var');
      var t = Classtype.GetVariable1();
      alert(t);
  });
  </code></pre>
  
  <p>But today in my Javascript class, the teacher taught us the following:</p>
  
  <pre><code>function Cname(pvariable1, pvariable2)
  {
  //private members//
  var variable1 = pvariable1;
  var variable2 = pvariable2;
  
  //Getter
  
  this.Getvariable1=function() {
  return variable1;
   }   
  }
  
  $(document).ready(function(){
  var cname = new Cname('test1', 'test2');
  var r = cname.Getvariable1();
  alert(r);        
  });
  </code></pre>
  
  <p>Since I'm pretty new to Javascript I was wondering which way of declaring classes/encapsulating my code is the preferred way and why?</p>