I'm just starting to get my mind around this whole Object Oriented thing, so bear with me.
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:
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;
}
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:
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:
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;
}
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.
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:
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+"<br />");
document.write(bacon.oldprice+"<br />");
bacon.price = "Cheap";
document.write(bacon.price+"<br />");
document.write(bacon.oldprice+"<br />");
But the output I get is: Expenisive undefined Cheap undefined
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.