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

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);
}

 

相关文章:

  • 2022-01-17
  • 2021-04-06
  • 2022-03-09
  • 2022-12-23
  • 2021-12-02
猜你喜欢
  • 2021-10-23
  • 2022-01-26
  • 2022-12-23
  • 2021-12-14
  • 2022-01-08
  • 2021-11-29
  • 2021-12-19
相关资源
相似解决方案