用for in遍历数组的问题

http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea

The purpose of the for-in statement is to enumerate over object properties. This statement will go up in the prototype chain, also enumerating over inherited properties, a thing that sometimes is not desired.

for in 会把原型链上继承的属性也遍历出来,有其他方法可以遍历,比如最简单的下标遍历和Array的forEach方法

for (var i=0; i < arr.length; i++) {
    console.log(i)  
}

你可能感兴趣的:(用for in遍历数组的问题)