我注意到在我的 javascript 中,如果我创建一个空数组,将其作为关联数组循环,并打印出内容,它会返回看起来像数组对象类本身的函数的内容。这是我的代码:
var test = new Array();
for(var i in test){
document.write(i + " " + test[i] + "<br>");
}
alert(test.length); // this returns 0
上面的代码打印以下内容(我省略了一些输出,因为它有点长)
$family function (){return u; }
$constructor function Array() { [native code] }
pop function pop() { [native code] }
push function push() { [native code] }
reverse function reverse() { [native code] }
shift function shift() { [native code] }
sort function sort() { [native code] }
splice function splice() { [native code] }
unshift function unshift() { [native code] }
concat function concat() { [native code] }
join function join() { [native code] }
slice function slice() { [native code] }
indexOf function indexOf() { [native code] }
etc...
我注意到,如果我使用 for 循环遍历数组,即:
for(var i = 0; i < test.length; i++)
浏览器不会打印出任何内容(这是应该发生的事情)
任何人都可以解释为什么当我以另一种方式循环它时,我会从一个空数组中得到一堆函数?如果有必要的话,我使用的是 mootools v1.3。提前致谢。
I noticed that in my javascript, if I create an empty array, loop through it as an associative array, and print out the contents, it returns what looks like functions from the Array Object class itself. Here is my code:
var test = new Array();
for(var i in test){
document.write(i + " " + test[i] + "<br>");
}
alert(test.length); // this returns 0
The above code prints the following (I'm omitting some of the output since it's kind of long)
$family function (){return u; }
$constructor function Array() { [native code] }
pop function pop() { [native code] }
push function push() { [native code] }
reverse function reverse() { [native code] }
shift function shift() { [native code] }
sort function sort() { [native code] }
splice function splice() { [native code] }
unshift function unshift() { [native code] }
concat function concat() { [native code] }
join function join() { [native code] }
slice function slice() { [native code] }
indexOf function indexOf() { [native code] }
etc...
I noticed that if I loop through the array using a for loop ie:
for(var i = 0; i < test.length; i++)
the browser doesn't print out anything (which is what should happen)
Can anyone explain why I'm getting a bunch of functions from an empty array when I loop through it the other way? In case it matters, I'm using mootools v1.3. Thanks in advance.
发布评论
评论(2)
摆脱任何扩展
Array.prototype
的东西。扩展Array
或Object
等默认类型的原型是不好的,会导致类似的问题。在保留原型扩展的同时规避问题的简单方法是添加
if(!test.hasOwnProperty(i)) continue;
检查。 (如果该属性位于对象本身而不仅仅是其原型链中的某个位置,则 obj.hasOwnProperty(key) 为true
)除此之外,您不应该使用
for..in
在数组上迭代时循环 - 在这种情况下使用for(var i = 0; i 。
Get rid of whatever extends
Array.prototype
. Extending the prototype of default types likeArray
orObject
is bad and causes problems like that.The easy way to circumvent issues while keeping the prototype extensions is adding a
if(!test.hasOwnProperty(i)) continue;
check. (obj.hasOwnProperty(key)
istrue
if the property is on the object itself and not only somewhere in its prototype chain)Besides that, you shouldn't use
for..in
loops when iterating over arrays - usefor(var i = 0; i < array.length; i++)
in this case.聚会有点晚了,但我在试图找到一种方法来做到这一点时发现了这一点。
这就是我想出来的。
顾名思义,它将创建一个达到指定大小的空对象数组。
A little late to the party, but I found this while trying to find a way to do this.
This is what I came up with.
It will, as its name implies, create an array of empty objects up to a provided size.