JS_工厂模式

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<script>
    var page = page || {};
    page.dom = page.dom || {};
    page.dom.Text = function(){
        this.insert = function(where){
            var txt = document.createTextNode(this.url);
            where.appendChild(txt);
        };
    }
    page.dom.Link = function(){
        this.insert = function(where){
            var link = document.createElement('a');
            link.href = this.url;
            link.appendChild(document.createTextNode(this.href));
            where.appendChild(link);
        };
    }
    page.dom.Image = function(){
        this.insert = function(where){
            var im = document.createElement('img');
            im.src = this.url;
            where.appendChild(im);
        }
    }
    page.dom.factory = function(type){
        return new page.dom[type]
    }
</script>
</body>
</html>

工厂模式,抄自tamu大叔博客的

posted @ 2013-11-18 09:37  方方和圆圆  阅读(415)  评论(0编辑  收藏  举报

再过一百年, 我会在哪里?