Check out a free preview of the full Vanilla JavaScript Projects course

The "Date & Iterators Summary" Lesson is part of the full, Vanilla JavaScript Projects course featured in this preview video. Here's what you'd learn in this lesson:

Anjana discusses the limitations and problems with the JavaScript Date object and mentions that the TC39 standards body is working on a new alternative to address these issues. A few additional examples of iterable objects in JavaScript are also shared.

Preview
Close

Transcript from the "Date & Iterators Summary" Lesson

[00:00:00]
>> We were just talking about, well, all of the ways that Date can be tricky to work with and all of the things that we wish we had from other languages that other languages might have. But of course, all different languages kind of deal with dates and times differently and whatnot.

[00:00:17]
So a note, about the date object, or the D Date object and the date objects that we create with new. Pretty much everyone ever finds them problematic and inadequate for our lives as developers and our experience as humans with this thing called the web that we have to deal with now.

[00:00:42]
[COUGH] So, as you may have noticed on the MDN Date page, there's a little note that TC 39, which again is the standards body for ECMAScript Which is the official specification of the JavaScript language is working on it. [LAUGH] They're working on a new alternative because they have heard the people, the people have taken to the Internet and complained anonymously.

[00:01:15]
[LAUGH] Tthat Date is not meeting our need. So, if you click through some of these links, you will find fun sentences like, Date has been a long-standing pain point in ECMAScript. And there is a detailed look at some of the problems with date in a blog post called Fixing JavaScript Date, which has the most adorable little JavaScript, developer on it.

[00:01:42]
And so, yes, if you want to learn more about all of the ways that Date can go horribly, horribly wrong, click through these links that are on the Date reference in MDN, and you will hopefully find some fun or perhaps horrifying stories. Cool, okay, good point. Before we leave the land of iterables and iterators, there was also a question that came up around, are for of loops the only place that we can use iterables?

[00:02:18]
No, there's actually several different places where JavaScript syntax is expecting an iterable object. So if we go back to our amazingly interesting page, and scroll down the iterative protocol page. And if we scroll down we have some built-in iterables, like we saw array is one. And we also have, yeah, some different APIs that accept iterables, wow.

[00:02:53]
And we have some different syntax in JavaScript, some different statements and expressions that do look for an integral. For of is the foster child as it were,but we also have the spread operator dot dot dot. We could spread our clock out into a clocky array. There's also an even deeper rabbit hole within generators which is generators can yield successive values from another iterable with a yield star that is sort of like yield each of the things in this thing.

[00:03:39]
Which I deliberately didn't tell I'll tell you about so that we could do our exercise together. [LAUGH] But in any case, it does mean that you can do some fanciness there. YoungStar is one of those. So, essentially, these iterables pop up in different places. And, for example, the Spread Operator I think is one of the more useful relatively new syntactic additions to JavaScript.

[00:04:06]
It's very helpful, and we talked about it a little bit in JavaScript First Steps. But yeah, you can spread your own custom iterable objects if they implement the iterator protocol. And that's what's nice about it. Again, it's not just one method that you always have to call this method and then you have to pass it on callback or blah blah blah.

[00:04:27]
It can pop up in lots of different places because it is an accepted protocol in the language as opposed to a convention of naming a method a certain thing

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now