抽象类的主要作用:定义一个产品簇,声明必备的方法,如果子类没有重写就会抛出错误.
var abstructFactory = function(child,father){
if(typeof abstructFactory[father] === 'function'){
function tempClass(){};
tempClass.prototype = new abstructFactory[father]();
child.constructor = child;
child.prototype = new tempClass();
}else{
throw new Error('no abstructFactory');
}
}
//抽象工厂方法
abstructFactory.ball = function(){
this.title = '球';
}
abstructFactory.ball.prototype = {
info : function(){
return new Error("can't use abstructFactory function");
},
action : function(){
return new Error("can't use abstructFactory function");
}
}
//球类抽象类
var football = function(){
this.title = '足球';
}
abstructFactory(football,'ball');
football.prototype.info = function(){
console.log(this.title);
}
football.prototype.action = function(){
console.log('踢球');
}
//继承的子类
var obj = new football();
obj.info();
obj.action();