0
我想要的是以简单的方式替换实例属性,并在类内自己完成。所以我可以利用构造函数,而不必创建一个巨大的方法来更新。更新一个JS对象
function Champ(champ){
var instance = this
instance.id = champ.id
// PERSONAL
instance.name = champ.name
instance.lore = champ.lore
// ATTRIBUTES
instance.attr1 = champ.attr1
instance.attr2 = champ.attr2
instance.fitness = champ.fitness
// BARS
instance.energy = champ.energy
instance.stress = champ.stress
function update(new_champ){
instance = new Champ(new_champ);
}
this.location = "1"
this.update = update
}
// I will put in a simple way, how does it fail for me and how do I wanted it to behave
c = new Champ({energy: 1, stress : 1})
c.energy //=> 1 (OK)
c.update({ energy: 9, stress: 9})
c.energy //=> 1 (FAIL, I wanted it to be 9)
我想我是真的很幼稚,有没有好办法,它的工作做这种情况下更换里面的类的?
谢谢,还有很好的参考 – 2010-11-29 13:23:47