安全模式的工廠模式
阿新 • • 發佈:2018-09-19
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();
安全模式的工廠模式