js设计模式之安全工厂模式

$(function () {
    Factory('Java','TEXT')
});

var Factory=function(type,text){
    if(this instanceof  Factory){
         var s=new this[type](text);
        return s
    }else{
        return new Factory(type,text);
    }
};
Factory.prototype= {
    Java:function (text) {
        alert("JAVA")
    },
    JavaScript:function(text){
        alert("JavaScript")
    },
    UI:function (text) {
        alert("UI")
    }
}

这种模式好在降耦合,比简单工厂模式方便。

重点理解这个 

   new this[type]()

程序调用是这样的!

发布了52 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/liz9411/article/details/103614277
今日推荐