对这两个遍历的简单说明
这篇文章发布于 2023年12月01日,星期五,01:53。阅读 ? 次,? 条评论
遍历对象的可枚举属性。
const num = [1, 2]
Object.defineProperty(Object.getPrototypeOf(num), '2', {
value: 3,
enumerable: true,
writable: true
})
for (const key in num) {
console.log(num[key])
}
// 1
// 2
// 3遍历可迭代对象,输出内容与迭代器有关。
const obj = {
number: 4,
// [Symbol.iterator]: function () {
// let index = 1
// return {
// next: () => ({
// value: index,
// done: index++ == this.number
// })
// }
// },
[Symbol.iterator]: function* () {
for (let index = 1; index <