Array indexOf implementation for Internet Explorer
Posted
by Daemon
on Stack Overflow
See other posts from Stack Overflow
or by Daemon
Published on 2010-05-19T19:24:43Z
Indexed on
2010/05/19
19:50 UTC
Read the original article
Hit count: 250
There are plenty of solutions on how to get the indexOf implementation into the Array prototype so that it works under Internet Explorer, however I've stumbled upon an issue that doesn't seem to be addressed anywhere I've looked so far.
Using the pretty well agreed upon implementation at MDC, I have the following code that's being problematic now:
// indexOf support for IE (from MDC)
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0) ? Math.ceil(from) : Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this && this[from] === elt)
return from;
}
return -1;
};
}
var i = [1,2,3,4];
for (j in i)
{
alert(i[j]);
}
I am expecting to receive 4 alerts, each one containing one of the elements of the array. In Firefox and Chrome, that's exactly what I see, however in IE8 I get an additional alert containing the indexOf function code.
What can be done to avoid this?
© Stack Overflow or respective owner