标签:pre console 一个 style item val span instance 遍历
forEach函数遍历数组:
var arr = [1,2,3]; arr.forEach (function (item, index) { console.log (index,item) })
forEach函数遍历对象:
var obj = { x: 100, y: 200, z: 300 } var key; for (key in obj){ if (obj.hasOwnProperty (key)) { console.log (key,obj[key]) } }
能遍历二者的forEach函数
function forEach (obj, fn) { var key; if (obj instanceof Array) { obj.forEach (function (item, index) { fn (item,index) }) } else { for (key in obj) { if (obj.hasOwnProperty (key)){ fn (key, obj[key]) } } } } var arr = [1, 2, 3]; var obj = { x: 100, y: 200, z: 300 } forEach (arr, function (item, index) { console.log (index,item) }) forEach (obj, function (key, val) { console.log (key, val) })
标签:pre console 一个 style item val span instance 遍历
原文地址:http://www.cnblogs.com/yoyoy/p/8007449.html