我試圖爲我的項目進行一個非常簡單的繼承模式,從基類擴展到專門的類。但是,我的專門類的屬性被父類的屬性覆蓋。具有屬性的JS對象繼承
這是爲什麼,我該如何解決它?
感謝,
function Ship(className, x, y){
this.className = className;
this.x = x;
this.y = y;
this.speed = 0;
}
function Corvette(className, x, y){
this.className = className;
this.x = x;
this.y = y;
this.speed = 100;
Ship.call(this, className, x, y)
}
Corvette.prototype = Object.create(Ship.prototype);
var ship = new Ship("Biggie", 50, 50);
var corvette = new Corvette("Smallish", 50, 50);
console.log(Corvette.className) // "Smallish" - correct via parameter.
console.log(Corvette.speed) // should be 100, is 0 - not correct, "static" from parent attribute
console.log(Corvette.constructor.name) // Ship
保存你自己就是舊的「class」系統的頭痛,並使用[ES6 classes。](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) –
屬性查找通常發生在第一級反對prot (這個,className,x,y)'這個調用將用'0'代替速度的值。 –