title: creating a class to hold global variables in javascript
body: <p>I'm developing an Object oriented program in javascript. Everything works just fine, but the problem is that I have too many global variable in my code.</p>
  
  <p>The code :</p>
  
  <pre><code>var Item = function(name, price){
      this.name = name;
      this.price = price;
      this.sells = 0;
  }
  
  
  var sphagety = new Item ("sphagety", 130);
  var pizza = new Item("pizza", 150);
  var macaroni = new Item("macaroni", 100);
  var milk = new Item("milk", 20);
  var cheese = new Item("cheese", 15);
  
  // too many global variable
  var total = 0;
  var change = 0;
  var result = 0;
  var sellRecord = []; 
  var sumOfSell = [];
  
  var cash = {
      "sum" : [],
      "display" : function(disp){
  
          if (disp === total) {
               console.log("Total : Rp" + total);
  
          }
           else if(disp === result){
              console.log("subTotal : Rp" + result);
          }
          else if(disp === change){
              console.log("change : Rp" + change + "\nmuch obliged");
          };
  
          },
  
       "sell" : function(req, qty){
          for(var property in req)
          sells = req;
          sells.sells += qty;
          result = req.price * qty;
          total += result; 
          cash.save();
          cash.display(total);
          cash.display(result);
  
  
      },
      "void" : function(){
            var lastSell = sumOfSell.length;
            var lastRec = sellRecord.length;
            total -= sumOfSell[lastSell-1];
            delete sellRecord[lastRec-1];
            delete sumOfSell[lastSell-1];
      },
      "save" : function(){
          cash.total += result;
          sumOfSell.push(result);
          sellRecord.push({sells});
       },
       "change" : function(money){
  
  
       if(money &gt; total){
          change -= (money - total);
          cash.display(change);
      }
      else if(money == total){
           console.log("much obliged");
           }
  
      if(money &lt; result){
           alert("the money is didn't enough!!\n  " + (money - result) + "");
          }   
       }
   }
   //  transaction no 1
   cash.sell(pizza,3);
   cash.sell(pizza,3);
   cash.sell(milk,3);
   cash.sell(cheese,3);
  </code></pre>
  
  <p>Then I'm trying to get rid of the global variable, I have an idea by making new class object called Session class and adding method endSession to cash Object, but I ended up stuck at figuring out how to handle closure between class.
  I modified the code like this</p>
  
  <pre><code>// new Class (Session class) put the global variable inside
  function Session (total, change, result, sellRecord, sumOfSell){
  this.total = total;
  this.change = change;
  this.result = result;
  this.sellRecord = sellRecord;
  this.sumOfSell = sumOfSell;
  }
   var cash = {
      "sum" : [],
  
      "display" : function(disp){
  
          if (disp === total) {
               console.log("Total : Rp" + total);
  
          }
           else if(disp === result){
              console.log("subTotal : Rp" + result);
          }
          else if(disp === change){
              console.log("change : Rp" + change + "\nmuch obliged");
          };
  
          },
  
       "sell" : function(req, qty){
          for(var property in req)
          sells = req;
          sells.sells += qty;
          Session.result = req.price * qty;
          Session.total += result; 
          cash.save();
          cash.display(total);
          cash.display(result);
      },
      // adding new cash method 
      "startSession" : function(){
          function session(){
              var session = {};
              var i = 1;
              session[i] = new Session(0,0,0,[],[]);
              //add
          }
       },
      "void" : function(){
            var lastSell = sumOfSell.length;
            var lastRec = sellRecord.length;
            total -= sumOfSell[lastSell-1];
            delete sellRecord[lastRec-1];
            delete sumOfSell[lastSell-1];
      },
      "save" : function(){
          cash.total += result;
          Session.sumOfSell.push(result);
          Session.sellRecord.push({sells});
       },
       "change" : function(money){
  
  
       if(money &gt; total){
          change -= (money - total);
          cash.display(change);
      }
      else if(money == total){
           console.log("much obliged");
           }
  
      if(money &lt; result){
           alert("the money is didn't enough!!\n  " + (money - result) + "");
          }   
       }
   }
  </code></pre>
  
  <p>I wonder if my idea is possible to do?</p>