I am learning about DOM collections. The textbook I’m using had the following Javascript code for iterating through the elements of the links collection:
var linksList = document.links;
for (var i = 0; i < linksList.length; ++i)
{
...
[code to print links to HTML paragraph]
...
}
Just for fun I changed the loop to a for ... in style loop:
for (var i in linksList)
and afterwards noticed that my script was now generating 3 more links than before, and each of them was “undefined.”
1) What are these undefined links, and why are they accounted for in a for ... in loop, but not the loop governed by document.links.length? (obviously length is 3 shorter than “true” length which includes the 3 mystery links)
2) How do I view collection using developer tools? If I could do this, perhaps I could answer the previous question myself.
Thanks!
Actually,
document.linksreturns an array-like object, that while providinglengthproperty and supporting indexing, has other properties, that show up infor inloop.