How do I change the string representation of an object instance in nodejs debug console. Is there a method (like toString()
in .NET) I can override?
Consider the following code:
class SomeObject{
constructor(){
this._varA = "some text";
this._varB = 12345;
this._varC = "some more text";
this._varD = true;
this._varE = 0.45;
}
toString(){
return "custom textual rapresentation of my object";
}
}
var array = [];
array.push(new SomeObject());
array.push(new SomeObject());
array.push(new SomeObject());
console.log(array);
However in other environments and programming languages I have worked on, overriding the toString()
method would show the result of toString()
(in the example above "custom textual representation of my object"
) instead of the dynamic textual representation created by the debugger (in the example code above this is: SomeObject {_varA: "some text", _varB: 12345, _varC: "some more text", …}
) - which I don't doubt for one minute it is very useful when a custom alternative is not defined.
I also realise that console.log(array.toString());
or even console.log(array.map(t=>t.toString()));
will produce something similar to what I am after, however this then prevents me to navigate through the objects using the debug navigation ie. drill into the object graph.
If this is not possible, would others benefit from this? If there is enough interest, I can look into defining and implementing it as a feature.