I've been struggling with the loosely-typed nature of JavaScript lately. I've been writing a program that makes API calls to fetch data, and stores the resulting data in a custom class. One of the values of the custom class is a date, which is supposed to be of the Date
type. Before creating the object, I always convert the date value to a proper Date
object before passing it into the constructor.
For reference, this is what the class looks like:
class Item {
constructor(title, date) {
this.title = title;
this.date = date;
}
}
However, when I attempt to retrieve the date value and compare it to another date value later on, it seems to have reverted to a normal object type, as the console claims item.date.compare
isn't a function. Logging has indicated the type of the object is a generic object, but that it's an instance of Date
. But I haven't been able to figure out how to treat it as specifically a Date
object.
As of now I've tried casting, which hasn't worked. Namely, I've tried doing item.date as Date
as well as Object.create(Date, item.date)
. Neither has worked so far. What am I missing?