function Ammo() {
var a = 0;
this.get_ammo = function() {
return a;
};
this.add_to_ammo = function() {
a = a+1
return a;
};
this.clean_ammo = function() {
return a=0;
}
}
function Weapon() {
var a =0;
}
function Gun() {
var a = 0;
this.fire = function(){
console.log("Bang");
}
}
Weapon.prototype = new Ammo();
Weapon.prototype.constructor = Weapon();
Gun.prototype = new Weapon();
Gun.prototype.constructor = Gun();
var a = new Ammo();
var w = new Weapon();
var g = new Gun();
a.add_to_ammo()
a.add_to_ammo()
a.add_to_ammo()
console.log(w.get_ammo())
// At this point I get 0, as expected. But after
w.add_to_ammo()
w.add_to_ammo()
w.add_to_ammo()
console.log(g.get_ammo())
// But here I get 3!
누군가는 설명 할 수 이유 나는 그 g 그래서, 독립적으로 자신의 분야이며, w를하는 개체를 생각 3
console.log(g.get_ammo())
후 얻을.
또한 내가 변경하는 경우var a = 0;
this.a = 0;
에 나는 결과를 예상 얻을 것을 알아 냈다. 개체의 필드는 부모 필드에 바인딩되지 않습니다.