码迷,mamicode.com
首页 > 其他好文 > 详细

for...in/ of, forEach 循环

时间:2017-02-20 13:22:35      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:不能   str   --   var   chrome   for   foreach   break   ons   

ES5
forEach缺陷:
不能用break语句跳出循环且不能在这个封闭的函数内使用return语句。
for in // 比较适用在对象上面的一个循环遍历,但是数组也可以用

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
}

for...in/ of, forEach 循环

标签:不能   str   --   var   chrome   for   foreach   break   ons   

原文地址:http://www.cnblogs.com/Zhou-HaHa/p/6418677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!