Whenever I create objects in JavaScript and output them to the console, I typically see a JavaScript object displayed. For instance:
var myObj = {
bla: "foo"
}
console.log(myObj);
This code will display:
{
bla: "foo"
}
However, when it comes to the Date
object, the behavior is different.
When I create a Date object, it automatically prints a string representation of the date.
For example:
var myObj = {
date: new Date()
}
console.log(myObj);
The output will be:
{
date: Sun Oct 25 2020 18:36:19 GMT-0700 (GMT-04:00)
}
What is the underlying mechanism at play here? How can I achieve similar behavior with my own custom objects?