在mysqljs ,连接查询的返回值打印为
[ RowDataPacket { column: "value"} ]
相当于 [ { column: "value"} ]
。
库如何为 { column: "value"}
对象指定一个“名称”RowDataPacket
?
我认为这可能与类有关,但下面不起作用
class RowDataPacket {
constructor() {
self.column = "value";
}
}
console.log(new RowDataPacket())
最佳答案
无论如何,您的类
解决方案确实在 Chrome 的开发工具中有效;不,它出现在 Firefox 的开发工具中。 (请注意,您在 constructor
中使用 self
的地方需要 this
,但它不会影响 devtools 中的结果,除了属性不存在。)
class RowDataPacket {
constructor() {
this.column = "value";
}
}
console.log("one", new RowDataPacket());
console.log("array of one", [new RowDataPacket()]);
(Look in the real console.)
如果愿意,您可以更进一步,也可以通过设置 @@toStringTag
将其应用于单个对象Object.prototype.toString
使用的对象(例如,类的原型(prototype))在为对象构建 [object XYZ]
字符串时,一些开发工具也使用它来识别对象的“类型”:
const myObject = {};
myObject[Symbol.toStringTag] = "myObject";
console.log(myObject);
console.log("Default toString", String(myObject));
(Look in the real console.)
在类原型(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.
关于javascript - 设置对象名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159272/