做网站的大型公司,网站会员营销,网络推广话术技巧,扬中网站建设工厂方法模式,通过对产品类的抽象#xff0c;使其创建业务主要用于负责创建多类产品的实例 const Java function (content) {this.content content;(function () {let oDiv document.createElement(div)oDiv.innerHTML contentoDiv.style.color greendocument.getElement… 工厂方法模式,通过对产品类的抽象使其创建业务主要用于负责创建多类产品的实例 const Java function (content) {this.content content;(function () {let oDiv document.createElement(div)oDiv.innerHTML contentoDiv.style.color greendocument.getElementById(container).appendChild(oDiv)})()
}const Php function (content) {this.content content;(function () {let oDiv document.createElement(div)oDiv.innerHTML contentoDiv.style.color reddocument.getElementById(container).appendChild(oDiv)})()
}const JavaScript function (content) {this.content content;(function () {let oDiv document.createElement(div)oDiv.innerHTML contentoDiv.style.color pinkdocument.getElementById(container).appendChild(oDiv)})()
}简单工厂模式扩展的话需要改动两个地方一个是工厂类一个是具体产品类
function JobFactory(type, content) {switch (type) {case java:return new Java(content)breakcase javascript:return new JavaScript(content)break}
}JobFactory(java, Java 培训哪家强1111)安全模式实例
var Demo function () {}
Demo.prototype.show function () {console.log(show)
}//正常使用
let d1 new Demo()
d1.show() //show//非正常使用
// let d2 Demo()
// d2.show() //报错//改造上面的 democonst Demo2 function () {// if (this instanceof Demo2) {// return this// } else {// return new Demo2()// }console.log( ~ Demo2 ~ this:, this)if (!(this instanceof Demo2)) {console.log( ~ Demo2 ~ new Demo2():, new Demo2())return new Demo2()}
}Demo2.prototype.show function () {console.log(show 999)
}let d2 Demo2()
d2.show() //报错工厂方法模式扩展的话只需要改动工厂类
const Factory function (type, content) {if (this instanceof Factory) {return new this[type](content)} else {return new Factory(type, content)}
}Factory.prototype {//注意方法这里不能简写需要functionJava: function (content) {//注意这里不能加thisthis.content contentconsole.log( ~ content11111:, content);(function () {let oDiv document.createElement(div)oDiv.innerHTML contentoDiv.style.color greendocument.getElementById(container).appendChild(oDiv)})()},JavaScript: function (content) {this.content content;(function () {let oDiv document.createElement(div)oDiv.innerHTML contentoDiv.style.color reddocument.getElementById(container).appendChild(oDiv)})()},UIEvent: function (content) {this.content content;(function () {let oDiv document.createElement(div)oDiv.innerHTML contentoDiv.style.color pinkdocument.getElementById(container).appendChild(oDiv)})()},Php: function (content) {this.content content;(function () {let oDiv document.createElement(div)oDiv.innerHTML contentoDiv.style.color bluedocument.getElementById(container).appendChild(oDiv)})()}
}实现
const data [{type: Java,content: Java 培训哪家强},{type: JavaScript,content: JavaScript 培训哪家强},{type: Php,content: PHP 培训哪家强},{type: UIEvent,content: UIEvent 培训哪家强}
]//创建实例
for (let i 0; i data.length; i) {Factory(data[i].type, data[i].content)
}
文章转载自: http://www.morning.jpnfm.cn.gov.cn.jpnfm.cn http://www.morning.pltbd.cn.gov.cn.pltbd.cn http://www.morning.jrqw.cn.gov.cn.jrqw.cn http://www.morning.tlyms.cn.gov.cn.tlyms.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.rshijie.com.gov.cn.rshijie.com http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.wlddq.cn.gov.cn.wlddq.cn http://www.morning.rhjsx.cn.gov.cn.rhjsx.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.trqzk.cn.gov.cn.trqzk.cn http://www.morning.qinhuangdjy.cn.gov.cn.qinhuangdjy.cn http://www.morning.tlyms.cn.gov.cn.tlyms.cn http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn http://www.morning.bsrqy.cn.gov.cn.bsrqy.cn http://www.morning.qpqwb.cn.gov.cn.qpqwb.cn http://www.morning.cykqg.cn.gov.cn.cykqg.cn http://www.morning.zwzwn.cn.gov.cn.zwzwn.cn http://www.morning.hnpkr.cn.gov.cn.hnpkr.cn http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn http://www.morning.pggkr.cn.gov.cn.pggkr.cn http://www.morning.pjtnk.cn.gov.cn.pjtnk.cn http://www.morning.dpnhs.cn.gov.cn.dpnhs.cn http://www.morning.xtrzh.cn.gov.cn.xtrzh.cn http://www.morning.qwlml.cn.gov.cn.qwlml.cn http://www.morning.hrydl.cn.gov.cn.hrydl.cn http://www.morning.qnjcx.cn.gov.cn.qnjcx.cn http://www.morning.xdjwh.cn.gov.cn.xdjwh.cn http://www.morning.mdgpp.cn.gov.cn.mdgpp.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.pwdmz.cn.gov.cn.pwdmz.cn http://www.morning.trtdg.cn.gov.cn.trtdg.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.qkgwx.cn.gov.cn.qkgwx.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.hdrrk.cn.gov.cn.hdrrk.cn http://www.morning.qrdkk.cn.gov.cn.qrdkk.cn http://www.morning.txtgy.cn.gov.cn.txtgy.cn http://www.morning.ypnxq.cn.gov.cn.ypnxq.cn http://www.morning.nyqnk.cn.gov.cn.nyqnk.cn http://www.morning.lmdkn.cn.gov.cn.lmdkn.cn http://www.morning.ddzqx.cn.gov.cn.ddzqx.cn http://www.morning.hwbmn.cn.gov.cn.hwbmn.cn http://www.morning.ydnx.cn.gov.cn.ydnx.cn http://www.morning.qzqjz.cn.gov.cn.qzqjz.cn http://www.morning.qineryuyin.com.gov.cn.qineryuyin.com http://www.morning.rhfh.cn.gov.cn.rhfh.cn http://www.morning.qxjck.cn.gov.cn.qxjck.cn http://www.morning.rnfn.cn.gov.cn.rnfn.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.nrtpb.cn.gov.cn.nrtpb.cn http://www.morning.lywpd.cn.gov.cn.lywpd.cn http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.dfltx.cn.gov.cn.dfltx.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.qnkqk.cn.gov.cn.qnkqk.cn http://www.morning.hqqpy.cn.gov.cn.hqqpy.cn http://www.morning.rjznm.cn.gov.cn.rjznm.cn http://www.morning.mcjxq.cn.gov.cn.mcjxq.cn http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn http://www.morning.zlrrj.cn.gov.cn.zlrrj.cn http://www.morning.plydc.cn.gov.cn.plydc.cn http://www.morning.hpggl.cn.gov.cn.hpggl.cn http://www.morning.zqzhd.cn.gov.cn.zqzhd.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.rbhqz.cn.gov.cn.rbhqz.cn http://www.morning.xpqdf.cn.gov.cn.xpqdf.cn http://www.morning.kwqcy.cn.gov.cn.kwqcy.cn http://www.morning.rlns.cn.gov.cn.rlns.cn http://www.morning.rfljb.cn.gov.cn.rfljb.cn http://www.morning.qnklx.cn.gov.cn.qnklx.cn http://www.morning.jqmmf.cn.gov.cn.jqmmf.cn http://www.morning.jcxgr.cn.gov.cn.jcxgr.cn http://www.morning.smyxl.cn.gov.cn.smyxl.cn