21
關於此行的腳本:爲什麼要將原型的構造函數設置爲其構造函數?
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;
嗎?更重要的是,不這樣做的後果是什麼,在哪些情況下它最有用?