安全模式的工厂模式

var Factory = function(type,content){
        if (this instanceof Factory)
        {
            var s = new this[type](type,content);//运算符优先顺序
            return s;
        }else {
            return new Factory(type,content);
        }
    }

    Factory.prototype = {
        html:function(type,content){
            this.content = content;
            this.type = type;
            this.show = function(){
                console.log(this.type+this.content);
            }
        },
        php:function(type,content){
            this.content = content;
            this.type = type;
            this.show = function(){
                console.log(this.type+this.content);
            }
        }
    }
    var p1 = Factory('html','哪家强?');
    p1.show();
    var p2 = Factory('php','真的不错!');
    p2.show();

猜你喜欢

转载自www.cnblogs.com/jokes/p/9671141.html