JS对象的属性继承
JS object inheritance with attributes
我试图得到一个非常简单的继承模式为我的项目进行,从一个基类扩展到一个专门的类。但是,我的专门化类的属性正在被父类的属性覆盖。
这是为什么?我如何解决它?
谢谢,
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
为什么在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类。
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
您只需要在Corvette功能开始时移动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)
你应该先调用parentclass构造函数,然后重写属性,这样由Corvette设置的属性不会被父类改变,即:
function Corvette(className, x, y){
Ship.call(this, className, x, y)
this.speed = 100;
}
相关文章:
- 序列化数据属性中对象的最可靠方法
- 识别没有ID或特定属性的对象
- Javascript嵌套函数属性继承
- JavaScript-用唯一属性标识对象
- JavaScript-通过类似的属性查找对象,并将其推送到一个新的数组中
- 如何创建共享某些属性的对象
- JavaScript 属性继承
- 如何指定在执行控制台时要显示的属性.log对象
- 继承对象并将变量传递给它,javascript
- 如何按天、周、月对具有时间戳属性的对象进行分组
- 数.[属性] 是对象或构造函数的属性
- 如何使用第二个数组中对象的相关属性对对象数组进行排序
- 按属性对对象数组进行排序
- Javascript:如何制作一个包含4个具有相同属性的对象的数组
- Angular 1.4.8/JS:创建构造函数并将属性继承到第三个对象中
- 如何在 JavaScript 中使用继承与构造函数方法一起使用 返回具有私有属性的对象文本
- JavaScript将对象作为属性继承
- JS对象的属性继承
- 用nodejs封装继承对象的属性
- 如何在javascript的构造函数中重写继承对象的属性?