工厂模式
2017-11-22 本文已影响0人
猫久伴你入眠
let Fashi = function() {
this.skill = "法攻";
this.blood = 120;
this.hit = 14;
console.log(this)
}
let Zhanshi = function() {
this.skill = "物攻";
this.blood = 180;
this.hit = 12;
console.log(this)
}
let Tanke = function() {
this.skill = "肉盾";
this.blood = 280;
this.hit = 6;
console.log(this)
}
const Fact = {
creator: function(role) {
let roler;
switch (role) {
case "法师":
roler = new Fashi();
break;
case "战士":
roler = new Zhanshi();
break;
case "坦克":
roler = new Tanke();
break;
}
return roler;
}
}
//冻结对象
Object.freeze(Fact);
var roleList = ['战士', '法师', '坦克']
roleList.forEach(function(item, idx) {
new Fact.creator(item);
})
Zhanshi { skill: '物攻', blood: 180, hit: 12 }
Fashi { skill: '法攻', blood: 120, hit: 14 }
Tanke { skill: '肉盾', blood: 280, hit: 6 }