2016-08-12 167 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

保存你自己就是旧的“class”系统的头痛,并使用[ES6 classes。](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) –

+0

属性查找通常发生在第一级反对prot (这个,className,x,y)'这个调用将用'0'代替速度的值。 –

回答

0

你只需要在克尔维特功能开始移动Ship.call(this, className, x, y)

而且,接下来的时间,发布代码之前,检查它是正确的,你写console.log(Corvette),而不是console.log(corvette)

另一件事:你不需要重复PARAMS你不希望覆盖

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)

1

为什么你在child对象相同的性​​能,这是已经在parent's

我建议你做

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使用此功能ES6 classes一些功能。

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

你应该先调用父类构造器,然后覆盖特性,这样通过克尔维特设置的属性不会被父类,即改变:

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

}