for...in/ of, forEach 循環

ES5
forEach缺陷:
不能用break語句跳出循環且不能在這個封閉的函數內使用return語句。
for in // 比較適用在對象上面的一個循環遍歷,可是數組也能夠用chrome

var arry = [1, 2, 3, 4, 5]
for (index in arry) {
console.log(index) // 0,1,2,3,4
if (index === 3) {
break; // 不能夠被打斷
}
console.log(arry[index]) // 1,2,3,4,5
console.log(typeof index) // string
}數組


ES6
for of // 支持集合 ---試了一下chrome,如今是支持的函數


for (index of arry) {
console.log(index) // 0,1,2,3,4
if (index === 3) {
break; // 能夠被打斷
}
console.log(arry[index]) // 1,2,3,4,5
console.log(typeof index) // number
}對象

相關文章
相關標籤/搜索