var Main = function(){};
Main.prototype = {
'times' : 0,
'request': function(){},
...
};
var SubA = function(){};
SubA.prototype = new Main() // same with Object.create(Main.prototype);
SubA.prototype.constructor = SubA;
var SubB = function(){};
SubB.prototype = new Main() // same with Object.create(Main.prototype);
SubB.prototype.constructor = SubB;
var sub_a = new SubA();
sub_a.times = 1;
var sub_b = new SubB();
// Here sub_b.times is already 1, how?
當我改變sub
類的Main
性質也在發生變化,屬性它不應該停留在0
?
對於我來說,使用Chrome控制檯時它保持0? – 2015-03-02 15:52:22
不可複製。 – thefourtheye 2015-03-02 15:53:33
'sub_a.times = 1;'永遠不會(!)改變'sub_b.times',除非'sub_a === sub_b'。這與原型無關。 *爲屬性賦值*總是添加或更改對象本身的屬性(無論它是否存在於原型鏈中)。 – 2015-03-02 16:25:24