我叫console.log(Family.prototype.money)
,值为200,这证实asset
是功能Family
的原型。但是当我打电话给console.log(other.money)
时,其值为1000,这是我在原型之前分配的。这里有什么问题?它看起来像其他对象的原型不同于功能家族的原型,这与我从书“面向对象的Javascript”中读到的内容完全矛盾。为什么原型中的属性值不一致?
function Family(father, mother, children){
this.father = father,
this.mother = mother,
this.children = children,
this.num = function(){
return (this.children.length+2);
}
}
Family.prototype.money = 1000; // notice!!!!
var myFamily = new Family('Hung', 'Hong', ['Phuong', 'Lien', 'Hiep']);
var other = new myFamily.constructor('Lan', 'Linh', ['Tung']);
var money = other.money;
var asset = {
money: 200,
car: 2,
house: 10
}
Family.prototype = asset;
这并不矛盾 - 你要替换的对象的原型,但其他对象仍参照原始原型。不知道为什么你认为这个操作必须传播。 – zerkms 2015-01-21 01:59:30
'var a = 1,b = a; a = 2;'你现在期望'b'是'1'还是'2'? – zerkms 2015-01-21 02:00:16
我不能按照你的问题。 – zerkms 2015-01-21 02:09:09