首页 > 编程语言 > 详细

[Javascript] Create Your First Iterator in JavaScript

时间:2019-12-28 12:34:40      阅读:77      评论:0      收藏:0      [点我收藏+]

Iterators are the foundation of generators. Much of the misunderstanding around generators comes from the lack of understanding iterators. An iterator has a Symbol.iterator property with an object that contains a next method which defines what is output each iteration.

 

let i = 0

const next = () => ({
    value: i++,
    done: i > 10
})

const iterator = {
    [Symbol.iterator]() {
        return {
            next
        }
    }
}

for (let value of iterator) {
    console.log(value)
}

 

[Javascript] Create Your First Iterator in JavaScript

原文:https://www.cnblogs.com/Answer1215/p/12111166.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!