《JavaScript设计模式》读书笔记

第五章(1)工厂方法模式

2017-09-20  本文已影响0人  鹏禾呈

简单工厂把类写在工厂函数的外面,而实例化在工厂函数内部,添加一个类的步骤1、在工厂函数以外写好类;2.在工厂函数内部添加;

工厂方法模式 直接把类的本身写在工厂函数里;添加一个类的步骤:在工厂函数内部添加

varFactory=function(type, content) {

if(this instanceofFactory){

console.log(this);

vars=this[type](content);

returns;

}else{

return newFactory(type, content)

}

};

Factory.prototype={

Java:function(content) {

this.content= content;

(function(content) {

vardiv=document.createElement('div');

div.innerHTML= content;

div.style.color='green';

document.getElementById('container').appendChild(div)

})(content)

},

php:function(content) {

this.content= content;

(function(content) {

vardiv=document.createElement('div');

div.innerHTML= content;

div.style.color='yellow';

div.style.background='red';

document.getElementById('container').appendChild(div)

})(content)

},

Javascript:function(content) {

this.content= content;

(function(content) {

vardiv=document.createElement('div');

div.innerHTML= content;

div.style.background='pink';

document.getElementById('container').appendChild(div)

})(content)

},

UI:function(content) {

this.content= content;

(function(content) {

vardiv=document.createElement('div');

div.innerHTML= content;

div.style.border='1px solid red';

document.getElementById('container').appendChild(div)

})(content)

}

};

vardata=[

{type:'Javascript',content:'Javascript哪家强'},

{type:'Java',content:'Java哪家强'},

{type:'php',content:'php哪家强'},

{type:'UI',content:'UI哪家强'},

{type:'UI',content:'UI哪家强'},

{type:'Javascript',content:'Javascript哪家强'}

];

for(vari=0;i

Factory(data[i].type,data[i].content)

}

上一篇下一篇

猜你喜欢

热点阅读