我認爲最好的方式來證明我的意思是表明講述的故事本身的代碼...爲什麼在原型鏈上操縱屬性實際上是在對象上創建的?
function Animal() {this.lives=7;}
function Cat() {}
Cat.prototype = new Animal();
cat1 = new Cat();
for (var i in cat1) { if (cat1.hasOwnProperty(i)) { console.log(i);}}
//undefined --as expected since property lives exists in prototype chain, Cat.prototype.
//but when i do this
cat1.lives -= 1;
// and now if i run this again
for (var i in cat1) { if (cat1.hasOwnProperty(i)) { console.log(i);}}
// lives -- How?? I just manipulated property in proto chain i didnt do obj.val = 3; which would create a new property.
,公正地完成..如果我做
Cat.prototype.lives = 10;
然後
cat1.prototype.lives; // 6
什麼是'cat1.prototype.lives'?你的意思是'cat1.lives'? – thefourtheye
cat1沒有自己的生命財產,所以我想如果我做'cat1.lives'並獲得原始鏈中的財產,那麼如果我做'cat1.lives = 3;'然後我改變原始鏈中的財產。 –