工厂模式(1)

    var Java=function(content){
        this.content=content;

        (function(content){
            var div=document.createlement('div');
            div.innerHTML=content;
            div.style.color='green'
            document.getElementById('container').appendchild(div)
        })(content)
    }
    var Demo=function(){}
    Demo.prototype={
        show:function(){
            console.log("成功获取!")
        }
    }

    var d=new Demo()
    d.show() //成功获取

    var d=Demo()
    d.show() //undefined
    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={
        Java:function(content){

        },
        Javascript:function(content){

        },
        UI:function(content){
            this.content=content;
            (function(content){
                var div=document.createElement('div')
                div.innerHTML=content
                div.style.border='1px solid red'
                document.getElementById('container').appendChild(div)

            })(content);

        },
        php:function(content){

        }
    }

    var data=[
        {type:'Javascript',content:'Javascript哪家强'},
        {type:'Java',content:'Java哪家强'},
        {type:'php',content:'php哪家强'},
        {type:'ui',content:'UI哪家强'}
    ]

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

猜你喜欢

转载自blog.csdn.net/qq_26798533/article/details/120059094