javascript 中的语句是为....

发布于 2024-10-10 07:17:35 字数 380 浏览 1 评论 0原文

任何人都可以解释如何在 javascript 中使用 for...in 语句。我读过w3school的文章,但我认为它不太清楚。下面是代码,请解释一下:

<html>
<body>
<script type="text/javascript">
var x;
var mycars = new Array();
mycars[10] = "Saab";
mycars[20] = "Volvo";
mycars[30] = "BMW";

for (x in mycars)
{
document.write(mycars[x] + "<br />");
}
</script>
</body>
</html>

anyone can explain how to use for...in statement in javascript. I had read the w3school article but i think it is not so clear.Below is the code, please explain this:

<html>
<body>
<script type="text/javascript">
var x;
var mycars = new Array();
mycars[10] = "Saab";
mycars[20] = "Volvo";
mycars[30] = "BMW";

for (x in mycars)
{
document.write(mycars[x] + "<br />");
}
</script>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只是我以为 2024-10-17 07:17:35

for in 循环将迭代对象中的每个属性。

在您的示例中,x 变量将循环访问 mycars 对象中的每个属性。

如果您添加 mycars.expense = "Porsche";,它也会发现这一点。


请注意,正如MDC所述,for in 循环不应该用于循环普通数组:

虽然它可能很诱人使用
这是迭代 Array 的一种方式,
这是一个坏主意。这
for...in 语句
迭代用户定义的属性
除了数组元素之外,所以
如果修改数组的非整数
或非正属性(例如
添加 "foo" 属性
甚至通过添加方法或
财产给
Array.prototype),
for...in 语句将
返回您的用户定义的名称
除了数字之外的属性
索引。另外,由于顺序
迭代是任意的,迭代
数组不能访问其中的元素
数字顺序。因此最好
使用带有数字索引的传统 for 循环
迭代数组。相似的
甚至可能会使用论据来反对
完全使用 for...in (至少
没有 propertyIsEnumerable()
或 hasOwnProperty()
检查),因为它也会迭代
超过 Object.prototype (虽然
通常不鼓励,可以,如
Array.prototype 的情况,很有用
由用户扩展,其中没有
引起的命名空间问题
包含其他库
可能不会执行上述检查
这样的迭代以及它们在哪里
意识到这种扩展的影响
都会有自己使用的
迭代器,例如 for...in)。

A for in loop will iterate through every property in an object.

In your example, the x variable will cycle through every property in the mycars object.

If you add mycars.expensive = "Porsche";, it will find that too.


Note that, as stated by MDC, for in loops should not be used to loop through ordinary arrays:

Although it may be tempting to use
this as a way to iterate over an Array,
this is a bad idea. The
for...in statement
iterates over user-defined properties
in addition to the array elements, so
if you modify the array's non-integer
or non-positive properties (e.g. by
adding a "foo" property
to it or even by adding a method or
property to
Array.prototype), the
for...in statement will
return the name of your user-defined
properties in addition to the numeric
indexes. Also, because order of
iteration is arbitrary, iterating over
an array may not visit elements in
numeric order. Thus it is better to
use a traditional for loop with a numeric index when
iterating over arrays. Similar
arguments might be used against even
using for...in at all (at least
without propertyIsEnumerable()
or hasOwnProperty()
checks), since it will also iterate
over Object.prototype (which, though
usually discouraged, can, as in the
case of Array.prototype, be usefully
extended by the user where are no
namespacing concerns caused by
inclusion of other libraries which
might not perform the above checks on
such iterations and where they are
aware of the effect such extension
will have on their own use of
iterators such as for...in).

猫七 2024-10-17 07:17:35

首先,您创建一个包含 3 个项目(而不是数组)的对象

  var mycars = new Object();
  mycars[10] = "Saab";
  mycars[20] = "Volvo";
  mycars[30] = "BMW";

,其中 102030 是对象属性。
那么您想要浏览该对象,访问所有属性并显示与属性关联的每个值。

这是 [ for (variable in object) expression ] javascript 构造的地方干预:
变量将被设置为对象的第一个属性,然后是第二个属性,然后是最后一个属性。尝试

  for (v in mycars) alert(v);

看看它是如何工作的,还有这个

  for (v in mycars) alert("Property: "+v+", value: "+mycars[v]);

First you create an object with 3 items (and not an Array)

  var mycars = new Object();
  mycars[10] = "Saab";
  mycars[20] = "Volvo";
  mycars[30] = "BMW";

where 10, 20 and 30 are the object properties.
then you want to navigate through the object, visit all properties and display each value associated to a property.

This is where the [ for (variable in object) expression ] javascript construction intervenes:
The variable will be set to the first property of the object, then to the 2nd, then to the last. Try

  for (v in mycars) alert(v);

to see how it works, and this as well

  for (v in mycars) alert("Property: "+v+", value: "+mycars[v]);
穿透光 2024-10-17 07:17:35

for ... in 结构迭代 in 右侧对象内的每个元素。在您的例子中,for 语句下面的块会针对 mycars 中的每辆车执行一次。

The for ... in construction iterates over every element within the object on the right side of in. In your case, the block below the for statement is executed once for every car in mycars.

生活了然无味 2024-10-17 07:17:35

for in是一种埋藏bug以供后人发现的方式。正如已经大量指出的那样,如果应用于数组,它将循环遍历数组的所有元素、数组的长度以及附加到数组的任何其他数据。将其应用于普通对象会更好,但您仍然应该通过 hasOwnProperty 过滤索引。

最好使用框架提供的函数,例如 jQuery 的 $.each

for in is a way of burying bugs for later generations to discover. As has been copiously pointed out, if applied to an Array, it will loop through all the elements of the array, and the length of the array, and whatever other data happens to be attached to the array. Applying it to an ordinary object is better, but you still should still filter the indices through hasOwnProperty.

Better to use a framework-provided function like jQuery's $.each

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文