1

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())
Avery235
  • 4,756
  • 12
  • 49
  • 83
  • That's correct `RowDataPacket` is a class and in get array of `RowDataPacket` instances. – dfsq Nov 07 '17 at 13:33

1 Answers1

1

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.
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I made a typo, should be `this.column` instead of `self.column` (didn't throw error since `self` is valid keyword). Too much python... – Avery235 Nov 07 '17 at 14:03