Friday, 6 September 2013

for..in loop over an Array iterates also over the prototype functions

for..in loop over an Array iterates also over the prototype functions

I've got a serious bug, which I've never seen before. First of all I've a
simple Array:
var myArray = ["123", "456", "789"]
Now I want to iterate over this Array with a for..in - loop:
function mapData(list) {
for ( var i in list) {
var item = list[i];
if (item) {
// do something
}
}
}
After calling the method with mapData(myArray), firebug shows in the
debugger this:
Loop: i = 0; item = 123;
Loop: i = 1; item = 456;
Loop: i = 2; item = 789;
Loop: i = compare;
Loop: i = union;
Loop: i = remove;
Loop: i = select;
Loop: i = contains;
So I think that are the prototype functions. But why? Any Ideas?
As I mentioned, I've never seen this before...

No comments:

Post a Comment