1

私は次の実装を使用しています

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 ループを使用している場合、その動作は奇妙なものです

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 でも同じコードが正常に動作します。

4

1 に答える 1

1

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
});
于 2013-04-16T10:58:47.707 に答える