let vehicleFactory = function (sub,sup) {
if(typeof vehicleFactory[sup]===“function”){//判断该抽象方法是否创建
function F() {}
F.prototype = new vehicleFactorysup;//继承父类属性方法
// console.log(F.prototype);
let a = new F();
// console.log( a.type);
sub.constructor = sub;//将子类的原型指针指向子类
sub.prototype = new F();//子类继承父类属性方法
}else {
return 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(“not be”);
}
}
//继承抽象属性
let bmw = function (price) {
this.price = price;
}
vehicleFactory(bmw,‘Car’);
bmw.prototype.getPrice=function(){
return this.price;
}
let b = new bmw(10000);
console.log(b.getPrice());
console.log(b.type);