let iterator = function (arr, cb) { arr.forEach((item, ind) => { cb(ind, item) }) } iterator([1, 2, 3, 4, 5, 6, 7, 8, 9], (key, val) => { console.log(key, val); })
let obj = { name: 'gl', age: 20, gender: 'male' } //OK for (val in obj) { console.log(obj[val]); } //Uncaught TypeError: obj is not iterable for (val of obj) { console.log(val); }
let obj = { 0: "gl", 1: 20, 2: "male", length: 3, [Symbol.iterator]: Array.prototype[Symbol.iterator] } for (val of obj) { console.log(val); }