Javascript jQuery:;“类”;使用对象数组

Javascript jQuery:;“类”;使用对象数组,javascript,jquery,Javascript,Jquery,我第一次尝试在jQuery中创建一个类,但是出现了一些问题。 这是我代码的一部分: $.Tables.prototype = { lineColor: 1, items: [], mountTable: function(ObjItems) { trClass = ((this.lineColor%2) == 0) ? 'even' : 'odd'; html = '<tr role="row" class="' + trClass

我第一次尝试在jQuery中创建一个类,但是出现了一些问题。 这是我代码的一部分:

$.Tables.prototype = {
    lineColor: 1,
    items: [],

    mountTable: function(ObjItems) {
        trClass = ((this.lineColor%2) == 0) ? 'even' : 'odd';
        html = '<tr role="row" class="' + trClass + '">';

        $.each(ObjItems, function(key, val) {
            html += '<td>' + val + '</td>';
        });

        html += '</tr>';

        this.lineColor++;
        this.items.push(ObjItems); console.log(this.items);

        return html;
    }
}
$.Tables.prototype={
线条颜色:1,
项目:[],
挂载表:函数(ObjItems){
trClass=((this.lineColor%2)==0)?“偶数”:“奇数”;
html='';
$.each(对象、函数(键、值){
html+=''+val+'';
});
html+='';
这个.lineColor++;
this.items.push(ObjItems);console.log(this.items);
返回html;
}
}
控制台返回一个空数组,我不明白为什么。 但如果我使用“推”两次,它就会起作用:

$.Tables.prototype = {
    lineColor: 1,
    items: [],

    mountTable: function(ObjItems) {
        trClass = ((this.lineColor%2) == 0) ? 'even' : 'odd';
        html = '<tr role="row" class="' + trClass + '">';

        $.each(ObjItems, function(key, val) {
            html += '<td>' + val + '</td>';
        });

        html += '</tr>';

        this.lineColor++;
        this.items.push(ObjItems);
        this.items.push(ObjItems);
        console.log(this.items);

        return html;
    }
}
$.Tables.prototype={
线条颜色:1,
项目:[],
挂载表:函数(ObjItems){
trClass=((this.lineColor%2)==0)?“偶数”:“奇数”;
html='';
$.each(对象、函数(键、值){
html+=''+val+'';
});
html+='';
这个.lineColor++;
这个.items.push(ObjItems);
这个.items.push(ObjItems);
console.log(this.items);
返回html;
}
}
有人有想法为什么会这样

--
非常感谢

我想通过创建init方法解决了这个问题

init: function() {
    this.items = [];
},
并调用以下声明的方法:

var table = new $.Tables();
table.init();

谢谢。

你能用发布一个工作示例吗?我不知道为什么JSFIDLE工作得很好!jsfiddler和my files之间的区别在于,所有js代码都位于jsfiddler中的同一位置。