为什么节点列表包含额外的未定义项,这些项未反映在其长度属性中?
背景:
我在使用节点列表时遇到了一个非常奇怪的现象。我想使用 getElementsByClassName 或类似的东西,然后对其进行排序。我决定一种方法是迭代节点列表并将每个项目推送到数组并对数组进行排序。 (顺便说一句,这确实有效,但没有达到预期)。我尝试使用 for (var i in nodeList) 进行迭代,但它一直在最后几个未定义的项目上引发异常。奇怪的是我可以使用 for (var i = 0; i
for (var i in document.getElementsByTagName("span"))
console.count("items");
console.log(document.getElementsByTagName("span").length);
它计数为 items: 382
但长度为 380
。正如预期的那样,当我输入 document.getElementsByTagName("span")[380]
和 document.getElementsByTagName("span")[381]
时,它们返回未定义。这种奇怪的行为不会发生在数组上(当然,nodeLists 和数组是不同的,但这确实证明不是不同的 for 循环导致了问题)。
问题:
为什么 for(var i in nodeList)
构造在最后返回几个未定义项的 nodeList 上表现不同?
Background:
I came across a very strange phenomenon while working with a node list. I wanted to use getElementsByClassName or something similar and then sort it. I decided one way would be to iterate through the nodelist and push each item to an array and sort the array. (This did work by the way but not as expected). I tried using the for (var i in nodeList)
to iterate through, but it kept throwing an exception on the last few items, which were undefined. the weird part is I could instead use for (var i = 0; i < nodeList.length; i++)
to iterate through. I just tested it again and on a stackoverflow page I ran in my console the following code:
for (var i in document.getElementsByTagName("span"))
console.count("items");
console.log(document.getElementsByTagName("span").length);
It counted out to items: 382
but length gave 380
. As expected, when I entered document.getElementsByTagName("span")[380]
and document.getElementsByTagName("span")[381]
they came back undefined. This strange behavior does not occur on arrays (granted, nodeLists and arrays are different, but this does prove that it's not the different for loops causing the issue).
question:
Why does for(var i in nodeList)
constructs behave differently on nodeLists returning a couple of undefined items at the end?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
for in
迭代语句捕获的两个附加属性是:让我给你一个简单的例子。假设页面上有 3 个 SPAN 元素。
现在,
spans
是一个 NodeList 对象,其中包含 5 属性:前 3 个属性是索引,它们包含对这些属性的引用跨度元素。另外两个属性 - length 和 item - 是两个附加属性。所有NodeList对象都具有这两个属性。
for in
语句迭代 NodeList 对象的所有 5 个属性,这可能不是您想要的。因此,请使用常规的for
语句。The two additional properties that the
for in
iteration statement catches are:Let me give you a simple example. Let's say that there are 3 SPAN elements on the page.
Now,
spans
is a NodeList object which contains 5 properties:The first 3 properties are indexes and they contain references to those SPAN elements. The other two properties - length and item - are two additional properties. All NodeList objects have these two properties.
The
for in
statement iterates over all 5 properties of the NodeList object, which is probably not what you want. Therefore, use a regularfor
statement.for-in 迭代对象的所有可枚举属性,例如长度和项目(这是您的情况)。这是另外两个结果的来源。它还将枚举添加到对象原型的所有内容。
for 循环遍历数字索引,并且不考虑可枚举属性。这就是为什么使用前一种方式更可靠。
for-in iterates through the all enumerable properties of the object such as length and item (this is your situation). This is where two more results come from. It will also enumerate everything added to object prototype.
for loops through the numeric indeces and doesn't take into consideration enumerable properties. That is why it much more reliable to use former way.