title: Object property "Previous Value" in Javascript
body: <p>I'm just starting to get my mind around this whole Object Oriented thing, so bear with me.</p>
  
  <p>So, I saw an example of an object function (for Javascript) where you assign a new value and that value gets assigned as that property's actual value. Example:</p>
  
  <pre><code>function Meat (name, color, price) {
      function newPrice (new_price) {
          this.price = new_price;
      }
   this.name = name;
   this.color = color;
   this.price = price;
   this.newprice = newPrice;
   }
  </code></pre>
  
  <p>Fine and dandy. Now I have the ability to assign a price by using Bacon.price or Bacon.newprice.  Personally, this seems like the highest order of semantics, since functionally it does the same thing. But as an advocate for semantic coding, I'll let it slide. Here's what I want:</p>
  
  <p>When I change the value of Bacon.price, the object should include a function to store the old value in a property called oldprice automatically.  I played around with this, but got nowhere. Here's what I've tried so far:</p>
  
  <pre><code>function Meat (name, color, price) {
      function oldPrice () {
          this.oldprice = this.price;
      }
   this.name = name;
   this.color = color;
   this.oldprice = oldPrice;
   this.price = price;
   }
  
  ///////////////////////
  
  function Meat (name, color, price) {
      this.name = name;
      this.color = color;
      this.oldprice = this.price;
      this.price = price;
  }
  </code></pre>
  
  <p>More or less variations of that. I thought that referring to the already assigned value (which would be nothing the first time) before the new value got assigned would be enough. I know that the value (or lack thereof) is stored somewhere before it gets assigned, I just don't know how to refer to it to assign it to the oldvalue property before it gets wiped out.</p>
  
  <hr>
  
  <p>Okay, maybe the problem is with my eyes or fingers then. Because I thought I had tried the suggestions before asking (I know, I didn't mention them!)  Unless I'm really confused, this should work:</p>
  
  <pre><code>function Meat(price)
  {
  
      function setPrice(price)
      {
          this.oldprice = this.price;
          this.price = price;
      }
  
  this.price=setPrice;
  }
  
  bacon = new Meat()
  bacon.price = "Expensive";
  document.write(bacon.price+"&lt;br /&gt;");
  document.write(bacon.oldprice+"&lt;br /&gt;");
  bacon.price = "Cheap";
  document.write(bacon.price+"&lt;br /&gt;");
  document.write(bacon.oldprice+"&lt;br /&gt;");
  </code></pre>
  
  <p>But the output I get is:
      Expensive
      undefined
      Cheap
      undefined</p>
  
  <p>All okay until that last one.  Again, I am willing to accept bad eyesight and poor spelling as the root. But I can't figure it out.</p>