💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
1.实现next() ```js // 创建一个 switch case function funGenerator(count) { switch(count) { case 1: return { value: 1, done: false } case 2: return { value: 2, done: false } case 3: return { value: 3, done: false } case 'done': return {value: undefined, done: true } } } // 创建 generator 对象 const gen = function() { let count = 0 // 计数器 // 创建一个 next 方法 let next = function () { // 每次调用count就会+1 count++ if (count > 3) { count = 'done' } return funGenerator(count) // 根据 count 返回 funGenerator } return { next: next } } const test = gen() console.log(test.next()); console.log(test.next()); console.log(test.next()); console.log(test.next()); ```