Interesting fact for completeness: The JSON counterpart of toString() is toJSON(). If an object has a toJSON() method, it will be evaluated and its result will be stringified instead:
That sounds... useful? 🙈
But yeah, it's a feature that I as well only discovered after many years of JS experience and since have never used in production. It's pretty cool though.
Kinda depends on what you want.
Slight error in your example: Twilight is an Alicorn. Source ❤️
Haha, good catch! I'd update my post but I'm too lazy, so let's say I'm referencing the first season.
Interesting fact for completeness: The JSON counterpart of
toString()
istoJSON()
. If an object has atoJSON()
method, it will be evaluated and its result will be stringified instead:Nice! I wasn't aware of that!
Then we could potentially override that method to get
[object Object]
when converting it to JSON.That sounds... useful? 🙈
But yeah, it's a feature that I as well only discovered after many years of JS experience and since have never used in production. It's pretty cool though.
Oh, and also the result would in fact be
"[object Object]"
. The quotes are part of the serialized result (since it needs to be valid JSON).I mean, the specification is that the object gets converted to a string. About the actual content of the string, the OP said nothing 😝