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();
    }


});

 

上一篇:VS Code(Visual Studio Code)的安装与中文配置


下一篇:非常有趣的的免费API接口