用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)  
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容