如何在javascript中实现继承概念?
我最近开始学习JavaScript。但我没有任何项目经验。最近参加了关于JavaScript的采访: 他问了一个关于继承的问题:应该是这样的: 我需要一种叫做Vehicle的方法,另外两种方法,一种是两轮车,另一种是四轮车 如果我为下面这两种方法创建实例如何在javascript中实现继承概念?,javascript,inheritance,Javascript,Inheritance,我最近开始学习JavaScript。但我没有任何项目经验。最近参加了关于JavaScript的采访: 他问了一个关于继承的问题:应该是这样的: 我需要一种叫做Vehicle的方法,另外两种方法,一种是两轮车,另一种是四轮车 如果我为下面这两种方法创建实例 var two-wheeler1 = new two-wheeler("Shine","125 kph") ; 另一个例子如下 var four-wheeler1 = new four-wheller("audi","1000kph");
var two-wheeler1 = new two-wheeler("Shine","125 kph") ;
另一个例子如下
var four-wheeler1 = new four-wheller("audi","1000kph");
如果我打印
如果我打印
如何做到这一点。您能帮我一个忙吗?请查看以下代码,以便更好地理解JS中的OOP和继承:
function twoWheeler(brand, kmp) {
this.brand=brand;
this.kmp=kmp;
this.getWheeles=function() {
return "2 wheels"
};
this.getVehicleInfo=function() {
return "Brand Name: " + this.brand + " Speed: " + this.kmp + " Type: 2 wheeler."
};
}
function fourWheller(brand, kmp){
this.brand=brand;
this.kmp=kmp;
this.getWheeles=function() {
return "4 wheels"
};
this.getVehicleInfo=function() {
return "Brand Name: " + this.brand + " Speed: " + this.kmp + " Type: 4 wheeler."
};
}
fourWheller.fourWheller= new twoWheeler();
fourWheller.prototype.constructor=fourWheller;
var twoWheeler1 = new twoWheeler("Shine","125 kph");
var fourWheller1 = new fourWheller("Audi","1000kph");
console.log(twoWheeler1.getWheeles())
console.log(fourWheller1.getWheeles())
console.log(twoWheeler1.getVehicleInfo())
console.log(fourWheller1.getVehicleInfo())
想必面试官和他的公司正在用现代JSES6或打字脚本编程。如果没有,你应该找另一家公司面试。ES6为定义类和继承关系提供了简洁的语法:
class Vehicle {
constructor(brand, kmp) {
this.brand = brand;
this.kmp = kmp;
}
getWheels() { return `${this.nWheels} wheels`; }
}
class TwoWheeler extends Vehicle {
constructor(brand, kmp) {
super(brand, kmp);
this.nWheels = 2;
}
}
class FourWheeler extends Vehicle {
constructor(brand, kmp) {
super(brand, kmp);
this.nWheels = 4;
}
}
顺便说一句,在世界其他地方,我们称之为两轮摩托车或滑板车,以及四轮汽车。阅读所有文档=>请注意,可能重复的名称-作为一个名称是无效的,在你继续前进之前,明确地说,在你参加任何面试之前,请回顾你的JS学习材料,还要确保您理解方法和类之间的区别,并纠正JS中的变量命名规则。你是从四轮车推导出两轮车?请求的车辆级别在哪里?什么是fourWheeler.fourWheeler=新的twoWheeler;做为什么需要为自己的类在原型上设置自动发生的构造函数,对吗?顺便说一句,是wheeler,不是wheeler,是wheels,不是wheeles。我不是给面试官答案,我只是想教他关于OOP和继承的基本概念,这在我的答案中体现出来。@Hanif,我们在这里重写每个函数的属性,那么,从性能角度来看,这是否明智?是的,我认为是这样。@han如果在我的回答中存在,则不存在。您应该定义一个类车辆,然后从中派生两轮车和四轮车。任何常见的逻辑都应该在超类中。另外,通常您会在原型上定义方法,而不是在构造函数中。谢谢您的帮助。不仅是为了了解目的而进行的采访,您还可以帮助我如何使用ES5或Venila javascript实现这一目标。对我来说,这是很大的帮助。我请求你。谢谢
function twoWheeler(brand, kmp) {
this.brand=brand;
this.kmp=kmp;
this.getWheeles=function() {
return "2 wheels"
};
this.getVehicleInfo=function() {
return "Brand Name: " + this.brand + " Speed: " + this.kmp + " Type: 2 wheeler."
};
}
function fourWheller(brand, kmp){
this.brand=brand;
this.kmp=kmp;
this.getWheeles=function() {
return "4 wheels"
};
this.getVehicleInfo=function() {
return "Brand Name: " + this.brand + " Speed: " + this.kmp + " Type: 4 wheeler."
};
}
fourWheller.fourWheller= new twoWheeler();
fourWheller.prototype.constructor=fourWheller;
var twoWheeler1 = new twoWheeler("Shine","125 kph");
var fourWheller1 = new fourWheller("Audi","1000kph");
console.log(twoWheeler1.getWheeles())
console.log(fourWheller1.getWheeles())
console.log(twoWheeler1.getVehicleInfo())
console.log(fourWheller1.getVehicleInfo())
class Vehicle {
constructor(brand, kmp) {
this.brand = brand;
this.kmp = kmp;
}
getWheels() { return `${this.nWheels} wheels`; }
}
class TwoWheeler extends Vehicle {
constructor(brand, kmp) {
super(brand, kmp);
this.nWheels = 2;
}
}
class FourWheeler extends Vehicle {
constructor(brand, kmp) {
super(brand, kmp);
this.nWheels = 4;
}
}