`for of` JavaScript 语句的支持情况如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27384494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:25:22  来源:igfitidea点击:

How well is the `for of` JavaScript statement supported?

javascriptfor-loopecmascript-6browser-support

提问by TheBlackBenzKid

var nameArray = [

{ name: 'john', surname: 'smith'  },
{ name: 'paul', surname: 'jones' },
{ name: 'timi', surname: 'abel' },

];  

for (str of nameArray) {    
   console.log( str.name );

}

I want to know, how supported is for( item of array )in terms of browser support, mobile JavaScript support - I realize you cannot do greater than >and this is pure iteration?

我想知道,for( item of array )在浏览器支持、移动 JavaScript 支持方面的支持程度如何——我意识到你做不到greater than >,这是纯迭代?

I have just discovered this, is this as good as I hope it is?

我刚刚发现了这一点,这是否如我希望的那样好?

采纳答案by Gianthra

The classic way of doing this is as follows:

这样做的经典方法如下:

  for(var i = 0; i < nameArray.length; i++){
    var str = nameArray[i];
  }

This will give you the exact functionality of a "foreach" loop, which I suspect is what you're really after here. This also gives you the added benefit of working in Internet Explorer.

这将为您提供“foreach”循环的确切功能,我怀疑这正是您在这里真正想要的。这也为您提供了在 Internet Explorer 中工作的额外好处。

There is also extensive knowledge of the exact loop described in the MDN. At this time Android web and it seems not everything supports your method so check the compatibility list on that page; seems to be a future release of the new JavaScript that will probably have OOP inside it.

MDN 中描述的精确循环也有广泛的知识。此时 Android 网页似乎并非所有内容都支持您的方法,因此请检查该页面上的兼容性列表;似乎是新 JavaScript 的未来版本,其中可能包含 OOP。

回答by Amit Joki

MDN:

MDN:

While for...in iterates over property names, for...of iterates over property values.

for...in 迭代属性名称,for...of 迭代属性值。

The above is what for...ofloop does. The below is its current status.

以上是for...of循环的作用。下面是它的当前状态。

This is an experimental technology, part of the Harmony (ECMAScript 6) proposal. Because this technology's specification has not stabilized, check the compatibilitytable for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.

这是一项实验性技术,是 Harmony (ECMAScript 6) 提案的一部分。由于此技术的规范尚未稳定,请查看兼容性表以了解在各种浏览器中的使用情况。另请注意,随着规范的变化,实验技术的语法和行为可能会在未来版本的浏览器中发生变化。

回答by Scimonster

This is the ES6 for..ofloop. According to the MDN article i just linked, it's supported by several browsers (see there for exact versions), but not IE. Currently, several mobile browsers also support it.

这是 ES6for..of循环。根据我刚刚链接的 MDN 文章,它受多种浏览器支持(参见那里的确切版本),但不支持 IE。目前,一些移动浏览器也支持它。

回答by vincent gravitas

In the meantime, you could use something like this:

与此同时,你可以使用这样的东西:

for(element_idx in elements) {
    element = elements[element_idx];
    ...
}

for...inhas been standard since ECMAScript 1st Edition.

for...in自 ECMAScript 1st Edition 以来一直是标准的

回答by Niet the Dark Absol

It's not.

不是。

Even if it were, it would be inappropriate to use it on an array.

即使是这样,在数组上使用它也是不合适的。

For arrays, you should always use a traditional forloop.

对于数组,您应该始终使用传统for循环。

You can, however, spice it up a bit:

但是,您可以稍微调味一下:

for( var i=0, l=nameArray.length, str=nameArray[0];
     i<l;
     i++,str=nameArray[i]) {
  console.log(str.name);
}