【发布时间】:2012-03-09 17:38:12
【问题描述】:
关于这个脚本的一行:
function Vehicle(hasEngine, hasWheels) {
this.hasEngine = hasEngine || false;
this.hasWheels = hasWheels || false;
}
function Car (make, model, hp) {
this.hp = hp;
this.make = make;
this.model = model;
}
Car.prototype = new Vehicle(true, true);
Car.prototype.constructor = Car;
Car.prototype.displaySpecs = function () {
console.log(this.make + ", " + this.model + ", " + this.hp + ", " + this.hasEngine + ", " + this.hasWheels);
}
var myAudi = new Car ("Audi", "A4", 150);
myAudi.displaySpecs(); // logs: Audi, A4, 150, true, true
我的问题是:什么是
Car.prototype.constructor = Car;
做吗?更重要的是,不这样做会有什么后果,在什么情况下最有用?
【问题讨论】:
标签: javascript