设置对象名称?
Set object name?
在 mysqljs 中,来自连接查询的 return 值打印为
[ RowDataPacket { column: "value" } ]
相当于[ { column: "value" } ]
。
库如何给{ column: "value" }
对象一个"name"RowDataPacket
?
我认为这可能与 类 有关,但以下内容不起作用
class RowDataPacket {
constructor() {
self.column = "value";
}
}
console.log(new RowDataPacket())
您的 class
解决方案 确实 工作,无论如何在 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.)
如果你愿意,你可以更进一步,也可以将它应用于单个对象,方法是在为对象构建 [object XYZ]
字符串时设置 @@toStringTag
of the object (for instance, the prototype of the class), which is used by Object.prototype.toString
,并且也被一些开发工具使用识别对象的"type":
const myObject = {};
myObject[Symbol.toStringTag] = "myObject";
console.log(myObject);
console.log("Default toString", String(myObject));
(Look in the real console.)
在 class 原型上使用它的示例:
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.
在 mysqljs 中,来自连接查询的 return 值打印为
[ RowDataPacket { column: "value" } ]
相当于[ { column: "value" } ]
。
库如何给{ column: "value" }
对象一个"name"RowDataPacket
?
我认为这可能与 类 有关,但以下内容不起作用
class RowDataPacket {
constructor() {
self.column = "value";
}
}
console.log(new RowDataPacket())
您的 class
解决方案 确实 工作,无论如何在 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.)
如果你愿意,你可以更进一步,也可以将它应用于单个对象,方法是在为对象构建 [object XYZ]
字符串时设置 @@toStringTag
of the object (for instance, the prototype of the class), which is used by Object.prototype.toString
,并且也被一些开发工具使用识别对象的"type":
const myObject = {};
myObject[Symbol.toStringTag] = "myObject";
console.log(myObject);
console.log("Default toString", String(myObject));
(Look in the real console.)
在 class 原型上使用它的示例:
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.