When you do console.log
it internally calls formatValue
in util.js
, which has a below check
const maybeCustomInspect = value[customInspectSymbol] || value.inspect;
Which means if your value has a inspect
method it gets called and then you can return the toString
in the same. So change your code to
class SomeObject{
constructor(){
this._varA = "some text";
this._varB = 12345;
this._varC = "some more text";
this._varD = true;
this._varE = 0.45;
}
inspect(depth, opts) {
return this.toString();
}
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);
Makes it print
[ custom textual rapresentation of my object,
custom textual rapresentation of my object,
custom textual rapresentation of my object ]
Nodejs has documentation on the same too
https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_custom_inspection_functions_on_objects
The inspect
method I used is deprecated as per documentation and the correct way to do it is below
const util = require('util');
class SomeObject{
constructor(){
this._varA = "some text";
this._varB = 12345;
this._varC = "some more text";
this._varD = true;
this._varE = 0.45;
}
[util.inspect.custom](depth, options) {
return this.toString();
}
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);
Edit: 28th-Mar-2018
So I launched the script using below
$ node --inspect-brk=54223 test.js
Debugger listening on ws://127.0.0.1:54223/81094440-716b-42a5-895e-4ea2008e0dff
For help see https://nodejs.org/en/docs/inspector
And then ran a socat
forwarder using below
$ socat -v TCP-LISTEN:54222,fork TCP:127.0.0.1:54223
When you debug the variable array
in debugger, you get below output on the socat
terminal
The information is that reconstructed by debugger to give you a meaningful representation.
Now the v8
debug api has no idea that we want to represent it differently, like we did for console.log
. Now there may be something similar in V8
code that does something similar, but looking at the source code, I was not able to figure out there exists such a thing. So you may need to confirm from someone who has V8 debugger api knowledge, if something of this sort exists
If not the you need to construct something at the IDE level, which again is not a a easy thing to do