我试图为我的项目进行一个非常简单的继承模式,从基类扩展到专门的类。但是,我的专门类的属性被父类的属性覆盖。具有属性的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'代替速度的值。 –