In mysqljs, the return value from a connection query is printed as
[ RowDataPacket { column: "value" } ]
which is equivalent to [ { column: "value" } ]
.
How does the library give the { column: "value" }
object a "name" RowDataPacket
?
I thought it might be to do with classes but the below doesn't work
class RowDataPacket {
constructor() {
self.column = "value";
}
}
console.log(new RowDataPacket())
Your class
solution does work, in Chrome's devtools, at any rate; not, it appears, in Firefox's devtools. (Note that where you've used self
in constructor
you wanted this
, but it doesn't affect the result in devtools other than that the property isn't there.)
class RowDataPacket {
constructor() {
this.column = "value";
}
}
console.log("one", new RowDataPacket());
console.log("array of one", [new RowDataPacket()]);
(Look in the real console.)
You can take it further if you like, and also apply it to individual objects, by setting the @@toStringTag
of the object (for instance, the prototype of the class), which is used by Object.prototype.toString
when building the [object XYZ]
string for the object, and is also used by some devtools to identify the "type" of the object:
const myObject = {};
myObject[Symbol.toStringTag] = "myObject";
console.log(myObject);
console.log("Default toString", String(myObject));
(Look in the real console.)
Example using it on a class prototype:
class RowDataPacket {
}
RowDataPacket.prototype[Symbol.toStringTag] = RowDataPacket.name;
console.log("one", new RowDataPacket());
console.log("Default toString", String(new RowDataPacket()));
Look in the real console.