I'd like someone - preferably Jon Skeet but I'll accept answers from mere mortals - to explain the following basic JavaScript scenario to me. I have a function similar to the following:
myFunction()
{
var row;
for (var i = 0; i < collectionView.itemsAdded.length; i++)
{
row = this.collectionView.itemsAdded[i];
}
for (var i = 0; i < collectionView.itemsAdded.length; i++)
{
// this always logs as the last value it was given from the first loop. I expected it to give me the same variety of values it gave me in the first loop.
row.property1 = true;
}
}
Unexpectedly, the second use of row always results in whatever the final value the first for loop gave it. i.e. if collectionView.itemsAdded.length is 45, row will log as 44 (bc zero index) and stay 44 on every iteration of the second loop.
I falsely expected that calling row in the second for loop would be enough to give me the same results row gave me in the first for loop. I thought that for each iteration of the second loop, row would log as a different item - instead, it logs every time as this.collectionView.itemsAdded[44] which it got from the first loop.
In my head, I get it. But can anyone explain further what's happening here? If row is supposedly this.collectionView.itemsAdded[i] - why isn't it transferable to another loop? Why must I again declare row = this.collectionView.itemsAdded[i] in the second for loop if I want the same values?