var Java = function (content) {//将内容备份到content中,以备日后使用this.content = content;//创建对象时,通过闭包直接执行,讲内容按照需求的模式插入到页面(function () {var div = document.createElement(‘div‘);div.innerHTML = content;div.style.color = ‘green‘;document.getElementById(‘container‘).appendChild(div);})(content);}var Php = function (content) {//将内容备份到content中,以备日后使用this.content = content;//创建对象时,通过闭包直接执行,讲内容按照需求的模式插入到页面(function () {var div = document.createElement(‘div‘);div.innerHTML = content;div.style.color = ‘blue‘;document.getElementById(‘container‘).appendChild(div);})(content);}
var jobFactory = function (type, content) {switch (type){case ‘java‘:return new Java(content);case ‘php‘:return new Php(content);}}
//安全模式创建的工厂类var Factory = function (type, content) {if(this instanceof Factory){var s =new this[type](content);return s;}else{return new Factory(type,content);}}//工厂原型中设置创建对象的基类Factory.prototype = {Java : function (content) {//将内容备份到content中,以备日后使用this.content = content;//创建对象时,通过闭包直接执行,讲内容按照需求的模式插入到页面(function () {var div = document.createElement(‘div‘);div.innerHTML = content;div.style.color = ‘green‘;document.getElementById(‘container‘).appendChild(div);})(content);},Php : function (content) {//将内容备份到content中,以备日后使用this.content = content;//创建对象时,通过闭包直接执行,讲内容按照需求的模式插入到页面(function () {var div = document.createElement(‘div‘);div.innerHTML = content;div.style.color = ‘green‘;document.getElementById(‘container‘).appendChild(div);})(content);}}
原文:http://www.cnblogs.com/endy-blog/p/407366e5e1ad8073d31e0cf62a9fa128.html