ECMAScript6 给对象实现 iterator 方法

来源:互联网 发布:网络流行文化 编辑:程序博客网 时间:2024/06/15 05:28
Object.prototype[Symbol.iterator] = function(){let index = 0,_this = this;return {next: function(){return index < _this.length ? { value: d[index++], done: false }:{ value: undefined, done:true };}}}
const d = {'0': 't','1': 'a','2': 'o',length: 3}var itD = d[Symbol.iterator]();console.log(itD.next());console.log(itD.next());console.log(itD.next());console.log(itD.next());