for for-in foreach 循环
var a = [1, 2, 3, 4, 5] //for用来便利数组元素非常简单 for (var i = 0; i < a.length; i++) { console.log(a[i]) //1,2,3,4,5 }
//for-in 能够更方便的便利对象属性成员 var person = { name: "XX", age: "20", sex: "男" } for (var k in person) { console.log(k); //获取对象属性 name age sex console.log(person[k]); //获取对象的属性值 XX 20 男 }
//forEach()为数组中的每一项运行指定函数 var b = [1, 2, 3, 4, 5]; b.forEach(function(value, index) { value++; console.log(value) // 2,3,4,5,6 });