工厂方法模式配音是说将实际创建对象推迟到子类中.这样核心类就成了抽象类.安全模式类可以屏蔽对类的错误操作而正确创建类
var factory = function(type,contant){
if(this instanceof factory){
var obj = new this[type](contant);
return obj;
}else{
return new factory(type,contant);
}
}
factory.prototype = {
basketball : function(contant){
this.info = '篮球';
this.contant = contant;
this.play = function(){
console.log('投篮')
}
},
football : function(contant){
this.info = '足球';
this.contant = contant;
this.play = function(){
console.log('射门');
}
}
}
var sport = factory('basketball','乔丹');
sport.play();
console.log(sport.contant);
var sport2 = new factory('football','梅西');
console.log(sport2.info);
sport2.play();