通过一个工厂对象的入参决定创建某一类的对象
//篮球基类
var Basketball = function () {
this.intro = 'basketball is from American';
}
Basketball.prototype = {
getSize: function () {
console.log('basketball is 32cm');
}
}
//足球基类
var Football = function () {
this.intro = 'football is from England';
}
Football.prototype = {
getSize: function () {
console.log('football is 20cm');
}
}
//运动工厂
var SportFactory = function (name) {
switch(name){
case 'basketball':
return new Basketball();
case 'football':
return new Football();
default:
console.log('para error!');
}
}
var fo = SportFactory('football');
console.log(fo.intro); //football is from England
fo.getSize(); //football is 20cm