javascript 中 for..in 和 for every..in 有什么区别?

发布于 2024-07-14 10:44:05 字数 88 浏览 5 评论 0原文

JavaScript 中的 for..in 和 foreach..in 语句有什么区别? 是否存在我不知道的细微差别,或者它是否相同但每个浏览器都有不同的名称?

What is the difference between for..in and for each..in statements in javascript?
Are there subtle difference that I don't know of or is it the same and every browser has a different name for it?

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

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

发布评论

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

评论(4

因为看清所以看轻 2024-07-21 10:44:05

“for every...in” 在指定对象属性的所有值上迭代指定变量。

示例:

var sum = 0;
var obj = {prop1: 5, prop2: 13, prop3: 8};
for each (var item in obj) {
  sum += item;
}
print(sum); // prints "26", which is 5+13+8

来源

“for... in" 以任意顺序迭代对象的所有属性上的指定变量。

示例:

function show_props(obj, objName) {
   var result = "";
   for (var i in obj) {
      result += objName + "." + i + " = " + obj[i] + "\n";
   }
   return result;
}

来源


注释 03.2013,对于每个... in 循环已弃用。 MDN 推荐的“新”语法是 对于...的

"for each...in" iterates a specified variable over all values of the specified object's properties.

Example:

var sum = 0;
var obj = {prop1: 5, prop2: 13, prop3: 8};
for each (var item in obj) {
  sum += item;
}
print(sum); // prints "26", which is 5+13+8

Source

"for...in" iterates a specified variable over all properties of an object, in arbitrary order.

Example:

function show_props(obj, objName) {
   var result = "";
   for (var i in obj) {
      result += objName + "." + i + " = " + obj[i] + "\n";
   }
   return result;
}

Source


Note 03.2013, for each... in loops are deprecated. The 'new' syntax recommended by MDN is for... of.

贵在坚持 2024-07-21 10:44:05

这个演示有望说明其中的差异。

var myObj = {
    a : 'A',
    b : 'B',
    c : 'C'
};
for each (x in myObj) {
    alert(x);        // "A", "B", "C"
}
for (x in myObj) {
    alert(x);        // "a", "b", "c"
    alert(myObj[x]); // "A", "B", "C"
}

This demonstration should hopefully illustrate the difference.

var myObj = {
    a : 'A',
    b : 'B',
    c : 'C'
};
for each (x in myObj) {
    alert(x);        // "A", "B", "C"
}
for (x in myObj) {
    alert(x);        // "a", "b", "c"
    alert(myObj[x]); // "A", "B", "C"
}
这个俗人 2024-07-21 10:44:05

阅读优秀的 MDC 文档。

第一个用于正常循环集合并任意改变对象的属性。

for...in 循环不会迭代内置属性。 其中包括对象的所有内置方法,例如String的indexOf方法或Object的toString方法。 但是,循环将迭代所有用户定义的属性(包括任何覆盖内置属性的属性)。

for...in 循环以任意顺序迭代对象的属性。 如果某个属性在一次迭代中被修改,然后在稍后的时间被访问,则循环公开的值将是其在稍后时间的值。 在访问之前被删除的属性以后将不会被访问。 添加到正在进行迭代的对象的属性可以在迭代中被访问或省略。 一般来说,除了当前正在访问的属性之外,最好不要在迭代期间从对象中添加、修改或删除属性; 无法保证添加的属性是否会被访问,修改的属性是否会在修改之前或之后被访问,或者删除的属性是否会在删除之前被访问。

后者允许您循环遍历对象的属性。

在对象属性的所有值上迭代指定变量。 对于每个不同的属性,都会执行指定的语句。

Read the excellent MDC documentation.

The first is for normal looping over collections and arbitrarily over an object's properties.

A for...in loop does not iterate over built-in properties. These include all built-in methods of objects, such as String's indexOf method or Object's toString method. However, the loop will iterate over all user-defined properties (including any which overwrite built-in properties).

A for...in loop iterates over the properties of an object in an arbitrary order. If a property is modified in one iteration and then visited at a later time, the value exposed by the loop will be its value at that later time. A property which is deleted before it has been visited will not then be visited later. Properties added to the object over which iteration is occurring may either be visited or omitted from iteration. In general it is best not to add, modify, or remove properties from the object during iteration, other than the property currently being visited; there is no guarantee whether or not an added property will be visited, whether a modified property will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.

The latter allows you to loop over an object's properties.

Iterates a specified variable over all values of object's properties. For each distinct property, a specified statement is executed.

如日中天 2024-07-21 10:44:05

除了其他答案之外,请记住 foreach...in 不是 ECMA 标准的一部分,也不包含在 即将推出的版本 3.1。 它是在 JavaScript 1.6 中引入的,它是 Mozilla 基金会的 ECMAScript3 扩展

根据链接的维基百科页面,它仅在 Firefox 1.5+ 和 Safari 3.x(+?) 中实现。

In addition to the other answers, keep in mind that for each...in is not part of the ECMA standard and also isn't included in the upcoming edition 3.1. It was introduced in JavaScript 1.6, which is an extension of ECMAScript3 by the Mozilla Foundation.

According to the linked Wikipedia page, it's only implemented in Firefox 1.5+ and Safari 3.x(+?).

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