zoukankan      html  css  js  c++  java
  • [Javascript] Create Your First Iterator in JavaScript

    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)
    }
  • 相关阅读:
    Swift 构造与析构
    Swift 协议
    Swift 扩展
    Swift 多态
    Swift 继承
    Swift 封装
    Swift 方法
    Swift 属性
    Swift 对象
    Swift 类
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12111166.html
Copyright © 2011-2022 走看看