0

我构建了一个处理页面的原型,我成功添加(推送),但可以获取数据,我失败了:

var foundImageIndex = Pages.indexFirst(function (item) { if (item.PageID == PageID) return true; });

这里是 javascript 页面处理程序:

var Pages = new Array();

PageContainer = function () //constructor for the proxy
{
   // this._baseURL = url;
};

PageContainer.prototype =
{

    AddPage: function (data) {
        if (data == null) return;
        Pages.push({ PageID: data.PageID, SegmentID: data.SegmentID });
    },

    GetPage: function (PageID) {
        alert('getPage('+PageID+')=' + JSON.stringify(Pages));

        var foundImageIndex = Pages.indexFirst(function (item) { if (item.PageID == PageID) return true; });

        var dt = { PageID: Pages[foundImageIndex].PageID, SegmentID: Pages[foundImageIndex].SegmentID };
        return dt;
    }


};

我从其他js调用如下:

var gPageContainer = new PageContainer();

 for (var i = 0; i < SegStruct.SegmentsCount; i++) {
                var segRClass = //get from webservice
                gPageContainer.AddPage({ PageID: i, SegmentID: segRClass.SegmentID });

            }

我试图调用:gPageContainer.GetPage(1); 但它在 GetPage: function (PageID)中失败,它在以下位置返回 -1: var foundImageIndex = Pages.indexFirst(function (item) { if (item.PageID == PageID) return true; });

foundImageIndex 总是 -1

为什么?

4

1 回答 1

0

只需在构造函数之前添加以下内容:

if (typeof Array.prototype.indexFirst == 'undefined') {
    Array.prototype.indexFirst = function (validator) {
        for (var i = 0; i <= this.length - 1; i++) {
            if (validator(this[i])) {
                return i;
            }
        }
        return -1;
    };
}
于 2012-05-05T10:21:18.023 回答