工厂模式


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 }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容