码迷,mamicode.com
首页 > 其他好文 > 详细

工厂方法模式(安全的工厂方法)

时间:2019-01-05 22:41:47      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:doc   data   factor   color   fir   function   confirm   工厂   按钮   

场景:页面中创建不同功能的按钮(添加、删除、提交、确认...)

var Factory = function(type,content){
    if(this instanceof Factory){
        var s = new this[type](content);
        return s;
    }else{
        return new Factory(type,content);
    }
}
Factory.prototype = {
    add:function(contetn){
        // add button
    },
    del:function(content){
        // del button
    },
    confirm:function(content){
        (function(content){
            var btn = document.createElement(‘button‘);
            btn.innerHTML = content;
            btn.style.color = red;
            document.getElementById(‘container‘).appendChild(btn);
        })(content)
    }
}
var data = [
    {type:‘add‘,content:‘添加‘},
    {type:‘del‘,content:‘删除‘},
    {type:‘confirm‘,content:‘确认‘}
];

for(var i = 3; i >= 0; i--){
    Factory(data[i],type,data[i].content);
}

 

工厂方法模式(安全的工厂方法)

标签:doc   data   factor   color   fir   function   confirm   工厂   按钮   

原文地址:https://www.cnblogs.com/wujiaqi/p/10226272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!