In JavaScript I override toString
on certain classes (using ES6 classes if it makes a difference), for example a subclass of an Error
object. When I do console.log(myObject)
, my toString
isn't getting called, it's instead using the default string output for that object.
It's not just console.log
that isn't using my function, but other places where the object should be converted to a string.
I have to explicitly do console.log(myObject.toString())
. Is this expected, or am I doing something wrong? I thought the purpose to toString
was to serve as the default string output.
Otherwise is there any point in overriding toString
? I could call my function customStringOutput
for example and do console.log(myObject.customStringOutput())
.
via user779159
No comments:
Post a Comment