ES6學習---迭代器的運用,自定義遍歷資料--for of
阿新 • • 發佈:2021-10-10
//宣告一個物件 const banji = { name: "終極一班", stus: [ 'xiaoming', 'xiaoning', 'xiaotian', 'knight' ], [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 (let v of banji) { console.log(v); }