包含MooTools的for..in的Javascript数组迭代 [英] Javascript array iteration using for..in with MooTools included

查看:101
本文介绍了包含MooTools的for..in的Javascript数组迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MooTools中迭代一个数组,但在使用简写 for..in 循环遍历数组时看到其他项目。当我使用常规的进行循环时,它工作正常。这是MooTools污染全局命名空间的问题还是我在这里做错了什么?

I am iterating over an array in MooTools but seeing additional items when iterating through the array using the shorthand for..in loop. It works fine when I use the regular for loop. Is this a problem with MooTools polluting the global namespace or am I doing something wrong here?

有一个 createTabs()迭代数组并为数组中的每个值创建一个选项卡的函数:

There is a createTabs() function that iterates over an array and creates a tab for each value in the array:

function createTabs() {
    var myTabs = [["First", "a.png"], ["Second", "b.png"]];
    for(var i in myTabs) {
        var tab = new Tab(myTabs[i][0], myTabs[i][1]);
        console.log(i);
    }
}

这是的输出console.log(i)

0
1
$family
each
clean
associate
link
contains
extend
getLast
getRandom
include
combine
erase
empty
flatten
hexToRgb
rgbToHex
toJSON

我理解前2个索引,但其余的来自哪里?

I understand the first 2 indexes, but where is the rest coming from?

编辑:谢谢快速回答Chetan和k Prime。这是有道理的,MooTools的 Array.each 添加是更清晰的迭代方式!

Edit: Thanks for the quick answers Chetan and k Prime. That makes sense, and the Array.each addition by MooTools is much cleaner way to iterate!

看起来很多现在更好:

myTabs.each(function(item) {
    var tab = new Tab(item[0], item[1]);
    console.log(item);
});


推荐答案

for..in 不适用于数组迭代。它迭代非内置对象的所有属性。由于MooTools为Array原型添加了更多功能,因此它们现在也是数组属性。请参阅此 https://developer.mozilla.org/En/Core_JavaScript_1 .5_Reference / Statements / For ... in

for..in is not meant for array iteration. It iterates over all the properties of an object that are not built-in. Since MooTools has added more functions to Array prototype, they are now array properties as well. See this https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Statements/For...in

只需使用基本for循环进行数组迭代。

Just use a basic for loop for array iteration.

这篇关于包含MooTools的for..in的Javascript数组迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆