2016-08-12 32 views
0

私のプロジェクトのための非常に単純な継承パターンを取得しようとしています。基底クラスから特殊クラスに継承しています。しかし、私の特殊クラスの属性は、親の属性によって上書きされています。属性を持つ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 
+0

保存あなた自身が古い "クラス"システムの頭痛を持ち、[ES6 classes。](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) –

+0

のプロパティルックアップは通常、最初のレベルから発生しますprotに反対する'Ship.call(this、className、x、y)'この呼び出しはスピードの値を '0'に置き換えます –

答えて

0

あなただけのコルベット機能の開始時にShip.call(this, className, x, y)を移動する必要があります。

また、次回は、コードを投稿する前に、それをチェックすることは正しい、あなたの代わりにconsole.log(corvette)

もう一つのconsole.log(Corvette)を書いた:あなたは

function Ship(className, x, y){ 
 
    this.className = className; 
 
    this.x = x; 
 
    this.y = y; 
 
    this.speed = 0; 
 
} 
 

 
function Corvette(className, x, y){ 
 
    Ship.call(this, className, x, y) 
 
    this.speed = 100;   
 
} 
 

 

 
Corvette.prototype = Object.create(Ship.prototype); 
 

 
var ship = new Ship("Biggie", 50, 50); 
 
var corvette = new Corvette("Smallish", 50, 50); 
 

 
console.log(corvette.className) 
 
console.log(corvette.speed) 
 
console.log(corvette.constructor.name)
を上書きしたくないのparamsあなたは繰り返す必要はありません

1

あなたはparent'sに既にあるchildオブジェクト内の同じ性質を持っているのはなぜ?

私はあなたが

function Ship(className, x, y, speed = 0) { 
 
    this.className = className; 
 
    this.x = x; 
 
    this.y = y; 
 
    this.speed = speed; 
 
} 
 

 
function Corvette(className, x, y, speed = 100) { 
 
    Ship.call(this, className, x, y, speed); 
 
} 
 

 
Corvette.prototype = Object.create(Ship.prototype); 
 
Corvette.prototype.constructor = Corvette; 
 

 
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 classesES6使用のいくつかの機能をサポートしている場合を行うことを示唆しています。

class Ship { // And also Ship is an abstractionm so you can use `abstract` keyword with it 
 
    constructor(className, x, y, speed = 0) { 
 
    this.className = className; 
 
    this.x = x; 
 
    this.y = y; 
 
    this.speed = speed; 
 
    } 
 
} 
 

 
class Corvette extends Ship { 
 
    constructor(className, x, y, speed = 100) { 
 
     super(className, x, y, speed); 
 
    } 
 
} 
 

 
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

+0

'Corvette.speed'は0であり、100ではなくopとなります) – rpadovani

+1

@rpadovani編集された –

0

あなたが最初parentclassのコンストラクタを呼び出し、次にプロパティをオーバーライドする必要があり、コルベットによって設定されたプロパティは、親クラスすなわちによって変更されることはありません。この方法:

function Corvette(className, x, y){ 
    Ship.call(this, className, x, y)  
    this.speed = 100;   

} 
関連する問題