javascript 简单工厂
function detail() { this.imgArr = []; this.codeArr = []; } detail.prototype.addimg = function(img) { this.imgArr.push(img); }; detail.prototype.addcode = function(code) { this.codeArr.push(code); }; detail.prototype.show = function() { $.each(this.imgArr, function(index, val) { $('#resDiv').html($('#resDiv').html() + val); }); $.each(this.codeArr, function(index, val) { $('#resDiv').html($('#resDiv').html() + val); }); }; var detailFactory = {}; var d1 = new detail(); d1.addimg('img1'); d1.addcode('code1'); var d2 = new detail(); d2.addimg('img2'); d2.addcode('code2'); detailFactory.df1 = d1; detailFactory.df2 = d2; $(function() { var r = Math.random(); if (r > 0.5) { detailFactory.df2.show(); } else { detailFactory.df1.show(); } });