A piece of code that abstracts the factory pattern in the book JavaScript Design pattern:
var VehicleFactory = function(subType, superType){
if(typeof VehicleFactory[superType] === "function"){
function F(){};
F.prototype = new VehicleFactory[superType]();
subType.constructor = subType; //
subType.prototype = new F();//
}else{
throw new Error("");
}
}
//
VehicleFactory.car = function{
this.type = "car"
}
VehicleFactory.car.prototype = {
getPrice:function(){
return new Error("")
}
}
//
VehicleFactory.Bus = function(){
this.type="Bus";
}
VehicleFactory.Bus.prototype = {
getPrice:function(){
return new Error("")
},
getPassengerNum:function(){
return new Error("");
}
}
//:
var BMW = function(price){
this.price = price
}
VehicleFactory(BMW, "car");
//
BMW.prototype.getPrice = function(){
return this.price
}
var bmw1 = new BMW(100000);
cosole.log(bwm1.getPrice());
question 1: what does it mean here? The constructor property should only be set on the prototype, and subType is the constructor of a class. Shouldn"t it be subType.prototype.constructor = subType? And this sentence should be put after subType.property = new F ()?
question 2: why is F used for transition here, and why can"t we just let subType.prototype = new VehicleFactory [superType] ()?