Nel nuovo framework che sto per pubblicare c'è una funzione che potrebbe fare al caso tuo:
codice:
Array.prototype.$forEach = function(callback, thisObj) {
var thisObj = thisObj || this,
value;
if(this.length == 0 && !this.item) {
for(var i in this) {
if(!this.constructor.prototype[i]) {
value = callback.call(thisObj, this[i], i, this) || null;
if(value !== null) {
if(value === "break") break;
if(value === "continue") continue;
}
}
}
} else {
for(var i = 0, l = this.length; i!=l; i++) {
if(i in this) {
value = callback.call(thisObj, this[i], i, this) || null;
if(value !== null) {
if(value === "break") break;
if(value === "continue") continue;
}
}
}
}
}
Non serve a normalizzare il forEach anche per IE ( nota che il metodo si chiama $forEach e non forEach ).
Il vantaggio di questa funzione rispetto all'originale è che funziona anche con array associativi senza dare problemi coi prototipi.
Attenzione però, se un array associativo contiene anche solo una voce con indice numerico tipo [0] o [1] incasini tutto...