SOURCE

// 使用 for of 遍历 Object
// 参考文献地址:https://juejin.cn/post/7217304466076631096

const obj = {
    name: '张三',
    age: 20,
    fav: '姑娘'
}

// 为 Object 对象,定义迭代器
Object.prototype[Symbol.iterator] = function () {
  const keys = Object.keys(this)
  let index = 0
  return {
    next: () => {
      const done = index >= keys.length
      const value = done ? undefined : this[keys[index]]
      index++
      return {
        done,
        value
      }
    }
  }
}

for(let item of obj) {
    console.log(item)
}
console 命令行工具 X clear

                    
>
console