码迷,mamicode.com
首页 > 编程语言 > 详细

[Javascript] Create Your First Iterator in JavaScript

时间:2019-12-28 13:10:50      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:value   under   which   com   define   nes   hat   pre   ext   

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

标签:value   under   which   com   define   nes   hat   pre   ext   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!