[英]How for…of Loop Statement Works in Javascript?(ECMAScript 6)
I have a snippet which I'm experimenting the for...of
statement on it: 我有一个片段,我正在试验它for...of
声明:
let arr = [3, 5, 7];
arr.foo = "hello";
for (let i in arr) {
console.log(i); // logs "0", "1", "2", "foo"
}
for (let i of arr) {
console.log(i); // logs "3", "5", "7"
}
My Question is that for...of
should run on iterable values, right? 我的问题是, for...of
应该在可迭代值上运行,对吗? so why the second for
doesn't print "hello"
? 所以为什么第二次for
不打印"hello"
?
Arrays are iterables over their elements . 数组是其元素的迭代。 That's how it's defined. 这就是它的定义方式。 That is how Array[Symbol.iterator]
is implemented. 这就是Array[Symbol.iterator]
的实现方式。
See http://www.2ality.com/2015/02/es6-iteration.html . 见http://www.2ality.com/2015/02/es6-iteration.html 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.