为什么JavaScript的For ... In循环不推荐用于数组? [英] Why is JavaScript's For...In loop not recommended for arrays?

查看:147
本文介绍了为什么JavaScript的For ... In循环不推荐用于数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某个地方读了(抱歉,我找不到链接),不推荐For ... In循环用于数组。这里说: http://www.openjs.com/articles/for_loop.php它适用于关联数组,适用于 http://www.w3schools.com/js/ js_loop_for_in.asp 用于迭代对象的所有属性(它没有说它可以在数组上使用)。我不知道该相信谁。我不希望这个问题成为辩论。我只是想知道我是否可以在我的代码中使用它而没有不可预见的副作用。谢谢!

解决方案

数组是一个对象,数组元素只是将数字索引转换为字符串的属性。例如,arr [123]引用数组对象arr中的属性123。



for ... in construct适用于所有对象,而不仅仅是数组,这是造成混淆的原因。



当有人 for ... in 一个数组,通常程序员打算只迭代所有元素,甚至很可能按顺序。例如,如果数组包含一堆数字,那么程序员很可能打算迭代一个数字流。语义与其他编程语言中的数组迭代非常相似,很容易混淆。



在JavaScript中,此构造不会按顺序迭代数组元素。它迭代所有数组的属性名称(包括继承的原型函数的名称,添加到它的任何属性,添加到它的任何其他非元素属性等),并且不按顺序排列所有。在早期的浏览器中,它甚至会找到属性 length ,尽管在​​最近的浏览器中,这些属性现在被定义为隐藏在这个确切的原因 - 人们不断绊倒它! / p>

使用上面的整数数组,您不会得到数字流,而是一串文本字符串。而不是元素值,而是属性的名称(它们只是不以任何顺序排列的数字索引)。如果他/她来自另一种编程语言,这很可能程序员的意思。如果存储在数组中的元素碰巧是类似的数值,那么它会让每个人都感到困惑。



这就是你不应该这样做的原因。你不应该使用看起来像它做一些明显的事情的语言结构,但实际上做的事情是完全不同的。它会产生非常模糊且很难找到的错误。


I read somewhere (sorry, I can't find the link) that the For...In loop is not recommended for arrays. It is said here: http://www.openjs.com/articles/for_loop.php that it is meant for associative arrays, and in http://www.w3schools.com/js/js_loop_for_in.asp that is for iterating through all the properties of an object (It does not say that it can be used on arrays). I do not know who to believe. I don't want this question to become a debate. I just want to know if I could use this in my code without unforeseen side effects. Thanks!

解决方案

An array is an object, and array elements are simply properties with the numeric index converted to string. For example, arr[123] refers to a property "123" in the array object arr.

The for ... in construct works on all objects, not just arrays, and that is cause for confusion.

When somebody for ... in an array, most often the programmer intends to iterate just all the elements, even most likely in order. For example, if the array holds a bunch of numbers, then the programmer most likely intends to iterate a stream of numbers. The semantics is so similar to array iteration in other programming languages that it is very easy to get confused.

In JavaScript, this construct does not iterate array elements in order. It iterates all the array's property names (including the names of inherited prototype functions, any properties added to it, any other non-element properties added to it etc.), and not in order at all. In earlier browsers, it will even find the property length, although in recent browsers these properties are now defined to be hidden for this exact reason -- people keep tripping on it!

With the array of integers above, you get not a stream of numbers, but a stream of text strings. And not the element values, but the names of the properties (which are just the numeric indices not in any order). This is most likely not what the programmer means, if he/she comes from another programming language. If the elements stored in the array happen to be similar numeric values, it confuses the hell out of everybody.

That's why you shouldn't do it. You shouldn't use a language construct that looks like it does obvious things, but actually does things that are completely different. It creates bugs that are very obscure and very difficult to find.

这篇关于为什么JavaScript的For ... In循环不推荐用于数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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