I am confused the following loop causing an infinite loop in IE8
for (var i in theArray) {
this.theArray.push(theArray[i]);
}
IE8 get caught in an infinite loop which I don’t understand why because this.theArray is a global array while theArray is a local variable.
If I had something like the following I would understand that an infinite loop would occurs:
for (var i in theArray) {
theArray.push(theArray[i]);
}
This only happens in IE8. Does IE8 treat variables and scoping differently?
EDIT
Here is what I have within an object
this.theArray = new Array();
this.selection = function(theArray) {
for (var i in theArray) {
this.theArray.push(theArray[i]);
}
}
EDIT
I found out that I am pass the global variable into the function as an argument. Duh! Why does this not work in IE8?
First of all, never use
for inloop on an array. It will iterate through the values as well as augmented properties.Then,
thiscannot be determined in your code.thismight refer to the global object. Also, you might have missed usingvarin the local variable, thus makingtheArraypoint to the same globaltheArrayyou are appending to.