Javascript中的对象属性“Previous Value”

时间:2009-03-09 21:02:38

标签: javascript variables object oop

我刚刚开始关注整个面向对象的事情,所以请耐心等待。

因此,我看到了一个对象函数(对于Javascript)的示例,其中您分配了一个新值,该值被指定为该属性的实际值。例如:

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;
 }

很好,很花哨。现在我有能力通过使用Bacon.price或Bacon.newprice来分配价格。就个人而言,这似乎是语义的最高顺序,因为在功能上它做同样的事情。但作为语义编码的倡导者,我会让它滑落。这就是我想要的:

当我更改Bacon.price的值时,该对象应包含一个函数,用于将旧值自动存储在名为oldprice的属性中。我玩弄了这个,但无处可去。这是我到目前为止所尝试的内容:

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;
}

或多或少的变化。我认为在分配新值之前引用已经分配的值(这将是第一次没有任何内容)就足够了。我知道它的值(或缺少值)在被分配之前存储在某个地方,我只是不知道如何引用它以在它被消除之前将它分配给oldvalue属性。


好吧,问题可能在于我的眼睛或手指。因为我以为我在问之前已经尝试了这些建议(我知道,我没有提到它们!)除非我真的很困惑,否则这应该有效:

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 />");

但我得到的输出是:     昂贵     未定义     廉价     未定义

一切都好,直到最后一个。再一次,我愿意接受视力不好和拼写错误的根源。但我无法理解。

8 个答案:

答案 0 :(得分:2)

这会在您致电obj.newPrice(xxx);时存储它 - 我认为您在使用obj.price = xxx;

时无法自动执行此操作
function Meat (name, color, price) {
    function newPrice (new_price) {
        this.oldprice = this.price
        this.price = new_price;
    }
 this.name = name;
 this.color = color;
 this.price = price;
 this.newprice = newPrice;
 }

编辑:

如@Mauris所述,Javascript 1.5支持getters and setters(另请参阅here - 您可以将其视为伪装成属性的方法。

使用这些,您的代码将如下所示:

function Meat (name, color, price) {
    function newPrice (new_price) {
        this.oldprice = this.price
        this.price = new_price;
    }
 this.name = name;
 this.color = color;

 this.__defineSetter__("price", function(newprice)
 {
       this.oldprice = this.price
       this.price = price;
 });
 }

不幸的是,Internet Explorer尚不支持此语法。

答案 1 :(得分:1)

是吗?

function setPrice(price) {
  this.oldprice = this.price;
  this.price = price;
}

答案 2 :(得分:1)

store = (function (prev) {
    callee = arguments.callee;
    return function (curr) {

        console.log("prev:" + prev, "curr:" + curr);
        store = callee(curr);
    }
}(0)) //Initial value

store(1); //Output : prev:0 curr:1 
store(2); //Output : prev:1 curr:2
store(3); //Output : prev:2 curr:3
store(4); //Output : prev:3 curr:4

答案 3 :(得分:0)

考虑一下你要做的事情:
记住旧价格 2.将当前价格更改为新价格

由于这是您想要做的两个步骤,您需要将它们粘贴在一个功能中。您需要先记住当前价格(在此.oldprice中),然后然后更改当前价格(this.price)。 RoBorg和cherouvim的回复都有代码来执行此操作。

答案 4 :(得分:0)

首先,我不太确定你正确地使用OOP Javascript。 OOP Javascript本身并不是面向对象的实际编程,而是基于一种称为原型的东西:

例如:

function Meat (name, color, price) {
       if(arguments.length > 0) 
    {
        this.init(name, color, price);
    }
}

Meat.prototype.init = function(name, color, price) {
    this.name = name;
    this.color = color;
    this.price = price;

    // Initially these are all set to null, 
    // because they don't have an old value yet.
    this.old_name = null;
    this.old_color = null;
    this.old_price = null;
};

这将构成你的构造函数。

接下来,我们需要添加一些方法,以便存储旧值:

Meat.prototype.setName = function(new_name) {
    // Store the old name of the meat
    this.old_name = this.name;
    this.name = new_name;
};

你会为你肉中的每一个属性写一个这样的方法。

答案 5 :(得分:0)

因为你刚刚开始使用OO javascript,请听听这个建议:

不要使用new关键字来创建对象。而是使用功能继承。这样您就可以轻松获取私有变量,我认为语法更容易。看看:

var meat = function(name, color, price){
   var that = {};
   var name = name;
   var price = price;
   var color = color;
   var oldPrice = null;
   var setPrice = function(p){
      oldPrice = price;
      price = p;
   };
   var getPrice = function(){ return price;};
   var getOldPrice = function(){return oldPrice;};
   that.getPrice = getPrice;
   that.setPrice = setPrice;
   that.getOldPrice = getOldPrice;
   return that;
};

你可以像这样使用上面的内容:

var m = meat("chicken", "white", 100);
m.setPrice(50);
var old = m.getOldPrice();

现在你可能需要getName和setName方法等。如果你想继承,你可以这样做:

var cow = function(name){
    var that = meat(name, "spotted", 100);
    that.moo = function() { return name +" moooo"; };
    return that;
}

无论如何,我可能已经回答了错误的问题,但也许你会读到这个并看到光明:)

答案 6 :(得分:0)

JS中还没有getter / setter或operator overloading(所以)你真的需要编写一个函数来设置价格并获得那些有趣的副作用。

答案 7 :(得分:0)

您正在覆盖您的价格变量。不要忘记,在javascript中,您可以为变量分配任何内容,因为它不是强类型的。所以,你设置它的方式:

function Meat(price)
{

    function setPrice(price)
    {
        this.oldprice = this.price;
        this.price = price;
    }

this.price=setPrice; //<-- this assigns the function "setPrice" to the variable "this.price"
}

bacon = new Meat()
bacon.price = "Expensive"; //<-- this overwrites the "price" variable with a string
document.write(bacon.price+"<br />");
document.write(bacon.oldprice+"<br />");
bacon.price = "Cheap";
document.write(bacon.price+"<br />");
document.write(bacon.oldprice+"<br />");

您可以(如上所述)使用getter和setter,假设您不需要IE支持。否则,请使用您定义的方法:

bacon = new Meat();
bacon.setPrice("Expensive");
document.write(bacon.price+"<br />");
document.write(bacon.oldprice+"<br />");
bacon.setPrice("Cheap");
document.write(bacon.price+"<br />");
document.write(bacon.oldprice+"<br />");

由于你真的想强制访问这些值,我也会为它们编写getter:

bacon.getPrice();
bacon.getOldPrice();