ES6迭代器自定义遍历数据
//声明一个对象 const banji = { name: "一班", stus: [ "xiaobai", "xiaohei", "xiaohua", "king" ], [Symbol.iterator]() { //声明索引变量 let index = 0; let _this = this; return { next: function() { if (index < _this.stus.length) { const result = { value: _this.stus[index], done: false }; index++; return result; } else { return { value: undefined, done: true } } } } } } //遍历这个对象 for (const v of banji) { console.log(v); }

浙公网安备 33010602011771号