【发布时间】:2018-09-06 02:07:31
【问题描述】:
所以我是 JavaScript 中的 OOP 新手,无法理解为什么我在以下代码中得到“未定义”,请帮助:
function Vehicle(energyType, energy) {
this.energyType = energyType;
this.energy = energy;
}
function Car(energyType, energy) {
Vehicle.call(energyType, energy);
this.doors = 4;
}
Car.prototype = Object.create(Vehicle.prototype);
Vehicle.prototype.run = function() {
console.log(`This vehicle is running on ${this.energyType}.`);
}
const c = new Car('gas', 80);
c.run();
当您运行代码时,它会显示“这辆车在未定义的情况下运行”,即使我说 Car has gas energyType...?
【问题讨论】:
标签: javascript oop inheritance prototype