数组indexOf和过滤器实现的IE给其他问题



我使用以下实现

if (!('forEach' in Array.prototype)) {
            Array.prototype.forEach= function(action, that /*opt*/) {
                for (var i= 0, n= this.length; i<n; i++)
                    if (i in this)
                        action.call(that, this[i], i, this);
            };
        }
        if (!('filter' in Array.prototype)) {
            Array.prototype.filter= function(filter, that /*opt*/) {
                var other= [], v;
                for (var i=0, n= this.length; i<n; i++)
                    if (i in this && filter.call(that, v= this[i], i, this))
                        other.push(v);
                return other;
            };

        }

但是当我使用for循环for ex时它的行为有点奇怪

var conditionJSONObject=new Array();
var conditionCombiner=new Array();
var combiner ;
combiner = jQuery.trim(jQuery(conditionTable.rows[i+1].cells[4].children[0]).select().val());
for(var index in conditionJSONObject)
{               
    if(conditionCombiner[index].combiner.toUpperCase() == 'AND')
    {/*Some code of mine*/}
}

这段代码给出了错误,虽然最初它工作得很好,同样的代码在FF上工作得很好。当我检查索引值是'ForEach'…是因为使用了原型吗?

Array.prototype.forEach = ...确实为所有数组添加了forEach属性。您可以在for(.. in ..)循环中避免这样做:

for(var index in array) {
    if(!array.hasOwnProperty(index)) continue;
    ...//loop body
}

或者你也可以像这样缩进:

array.forEach(function(value,index) {
    ...//loop body
});

相关内容

最新更新