1. 程式人生 > >安全模式的工廠模式

安全模式的工廠模式

return 工廠 () color content urn .sh class pre

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

安全模式的工廠模式