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
}对象

相关文章
相关标签/搜索