简单工厂模式
<script>
var factory=function(name,age,sex,national){
this.name=name
this.age=age
this.sex=sex
switch(national){
case "Chinese":
this.speak=function(){
console.log("我是"+this.name+"我今年:"+this.age+"我是中国人")
}
break;
case "England":
this.speak=function(){
console.log("我是"+this.name+"我今年:"+this.age+"我是英国人")
}
break;
}
}
var person=new factory("jack",15,male,"Chinese")
person.speak()
</script>
抽象工厂模式
<script>
function myCar(color,price){
this.color=color
this.price=price
}
myCar.prototype.run=function(){
throw new Error("我是摸具,具体实现自己决定" )
}
var BM=function(color,price){
myCar.call(this,color,price)
}
BM.prototype.run=function(){
console.log("我是宝马车我的颜色是"+this.color+"我的价格是"+this.price+"宝马正在跑")
}
new BM('red','1234').run()
var BC=function(color,price){
myCar.call(this,color,price)
}
BC.prototype.run=function(){
console.log("我是奔驰车我的颜色是"+this.color+"我的价格是"+this.price+"奔驰正在跑")
}
new BC('blue','99999').run()