javascript

    Metaprogramming

    02 Dec, 2023public

    Class in JS

    05 Dec, 2023public

    Promises

    13 Nov, 2023public

    Js notes

    11 Jan, 2024public

    Iteration in javascript

    15 Nov, 2023public
    economics

    Incentive

    21 Aug, 2023public
    demo

    Demo

    13 Nov, 2023public
    personal

    Teaching myself CS

    05 Dec, 2023public

    People

    12 Dec, 2023public

    Links

    25 Mar, 2024public
    computer graphics

    Computer graphics from scratch

    12 Dec, 2023public
    notag

    Flexbox practice

    14 Aug, 2024public
    Lying in wait, set to pounce on the blank page,are letters up to no good,clutches of clauses so subordinatethey'll never let her get away.
    - The Joy Of Writing, Wislawa Szymborska

    Iteration in javascript

    .17 Nov, 2023

    Let's understand iteration in js by making Range class.

    class Range {
      constructor(start,end) {
        this.start = start
        this.end = end
      }
      
      [Symbol.iterator]() {
        let i = this.start
        return {
          next: () => {
            if (i > this.end) {
              return {value: undefined, done: true}
            } 
            return {value: i++, done: false}
          }
        }
      }  
    }
    
    const r = new Range(1,10)
    
    for (let i of r) {
      console.log(i)
    }
    console.log("finished")
    

    Range class here is an iterable because it follows the iterable protocol. It's Symbol.iterator method returns an object which follows the iterator protocol.

    An object is said to be following the iterator protocol when it implements the next method. It's next method should return an object which conforms to the IteratorResult interface.

    Table of contents
    Abhimanyu