Latest JavaScript Features: Making Use of Iterators

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ก.ค. 2024
  • You may have heard of iterators, but have you created one? An iterator is a unique JavaScript construct, and we are going to take a look at it in this tutorial.
    Would you like to help keep this channel going?
    allthingsjavascript.com/contri...
    Access to EVERY course (get 2 months free): www.skillshare.com/r/profile/...
    Courses offered on Udemy at a discount (access from my site): allthingsjavascript.com/course...
    For more resources on JavaScript:
    www.allthingsjavascript.com
    #javascript #AllThingsJavaScriptLLC

ความคิดเห็น • 25

  • @gmjitendra
    @gmjitendra ปีที่แล้ว

    Excellent explanation. Thank you. I'm watching all other tutorials.

  • @arturoguillen
    @arturoguillen 4 ปีที่แล้ว +2

    Amazing content. You deserve more views, my friend.

  • @yasarhussain3811
    @yasarhussain3811 4 ปีที่แล้ว

    excellent

  • @debadipti
    @debadipti 4 ปีที่แล้ว

    thanks sir

  • @user-gg5nh1mf6l
    @user-gg5nh1mf6l 4 ปีที่แล้ว

    thanks

  • @21rogerwaters
    @21rogerwaters 4 ปีที่แล้ว +1

    Why are you using var instead const or let?

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว +1

      I try to use what best expresses my intention with whatever I'm defining. (Although, sometimes I will use var by default because I'm so used to it.) In this case I use var because these are declared on the global scope. If they were something I had no intention of changing I would use const. I usually use let inside a block. Simply personal preference on a lot of these reasons.

  • @fazzyakamello
    @fazzyakamello 4 ปีที่แล้ว +1

    A Question: Why should we consider using iterator as oppose to for loop? I didn't quite understand this bit...

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว +3

      A for loop iterates through all the values at once. If you don't need to iterate through all the values at once but need a value every so often, this is ideal. For example, lets say you need to grab a value from the array each time a button is clicked. A loop won't work for this, but an iterator will.

    • @KenzoArts
      @KenzoArts 4 ปีที่แล้ว

      ​@@AllThingsJavaScript But in order grab that value, you will need an if statement on array.next(), i guess ? I still don't get how that will not use a for loop, could you please clarify a bit? Thanks for wonderful tutorials

    • @fazzyakamello
      @fazzyakamello 4 ปีที่แล้ว

      All Things JavaScript, LLC ah ok makes sense!

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว

      @@KenzoArts You can grab that value without an if statement (it.next().value), but if you need to check the value, then yes you would need an if statement. Does that help?

    • @KenzoArts
      @KenzoArts 4 ปีที่แล้ว

      @@AllThingsJavaScript Excellent, it make now sense !! Thanks a lot for this wonderful tutorial

  • @aymanayman9000
    @aymanayman9000 4 ปีที่แล้ว

    Why they didn't implement it like python why all of this madness of has next and the object container in python you loop and if there's nothing next raise stop iteration error just like that

  • @akshay__sood
    @akshay__sood 3 ปีที่แล้ว

    Objects aren't iterable right?

    • @AllThingsJavaScript
      @AllThingsJavaScript  3 ปีที่แล้ว

      That is correct, but you can use something like Object.keys, Object.entries to create iterables of the properties.

    • @akshay__sood
      @akshay__sood 3 ปีที่แล้ว

      @@AllThingsJavaScript yes that can be done those methods are always there.
      Just wanted to confirm...
      Just curious to know how do you identify what is iterable and what is not?
      I know of a way, want to know of your's.

    • @AllThingsJavaScript
      @AllThingsJavaScript  3 ปีที่แล้ว +1

      @@akshay__sood So one practical way to tell is if you can use a for...of loop on it. Then it is an iterable. You can turn something into an iterable bay adding a method to it using Symbol.iterator. Technically that is what makes it iterable. Even though objects are not iterable by default, they can be made to be.

    • @akshay__sood
      @akshay__sood 3 ปีที่แล้ว

      @@AllThingsJavaScript exactly whenever you declare anything and do console.dir and if you can find Symbol.iterator then it is an iterable.

    • @AllThingsJavaScript
      @AllThingsJavaScript  3 ปีที่แล้ว

      @@akshay__soodSymbols are usually hidden, so that may not always work.