私のプロジェクトのための非常に単純な継承パターンを取得しようとしています。基底クラスから特殊クラスに継承しています。しかし、私の特殊クラスの属性は、親の属性によって上書きされています。属性を持つ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
保存あなた自身が古い "クラス"システムの頭痛を持ち、[ES6 classes。](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) –
のプロパティルックアップは通常、最初のレベルから発生しますprotに反対する'Ship.call(this、className、x、y)'この呼び出しはスピードの値を '0'に置き換えます –