如何解决Typescript:如何使用默认getter显示类的属性
export interface ISample {
propA: string;
propB: string;
}
export class Sample {
private props = {} as ISample;
public get propA(): string {
return this.props.propA;
}
public set propA(propA: string) {
this.props.propA = propA;
}
public get propB(): string {
return this.props.propB;
}
public set propB(propB: string) {
this.props.propB = propB;
}
}
在我的代码中,我使用该类按如下方式初始化对象。
let sample = new Sample();
sample.propA = 'A';
sample.propB = 'B';
但是当我尝试使用console.log(sample)
打印对象时,我得到了
props: {propsA: "A",propsB: "B"}
propsA: (...)
propsB: (...)
使用{propsA: "A",propsB: "B"}
时如何使输出仅显示console.log(sample)
?
PS:我将typescript 3.8.3
与Angular 9
一起使用。
解决方法
看到您希望不使用任何类对象实例而直接使用属性,可以完全跳过Sample
类。相反,您可以尝试直接使用ISample
界面。
尝试以下
export interface ISample {
propA: string;
propB: string;
}
export class AppComponent {
sample = {} as ISample;
ngOnInit() {
this.sample.propA = 'A';
this.sample.propB = 'B';
// prints {propA: "A",propB: "B"}
console.log(this.sample);
}
}
,
您的类Sample
具有一个名为sample
的类属性,该属性继承了ISample
。也就是说,很明显,您正在获取props: { propA: "A",propB: "B"}
的日志。
如果您希望propA
和propB
成为类的直接元素,则必须正确继承Interface。这样做,您必须将propA
和propB
设置为Sample
的直接元素,这会导致您需要的日志。
export class Sample implements ISample {
propsA = '';
propsB = '';
}
请记住,只要您使用private
状态,就必须相应地调整设置器和获取器。
您可以覆盖toString()方法:
export interface ISample {
propA: string;
propB: string;
}
export class Sample {
props = {} as ISample;
public get propA(): string {
return this.props.propA;
}
public set propA(propA: string) {
this.props.propA = propA;
}
public get propB(): string {
return this.props.propB;
}
public set propB(propB: string) {
this.props.propB = propB;
}
toString() {
return this.props;
}
}
let sample = new Sample();
sample.propA = 'A';
sample.propB = 'B';
console.log(sample.props)
,
您几乎无法通过console.log
来更改类的显示。此功能在浏览器中实现,几乎没有余地。例如implementation in FF
function log(aThing) {
if (aThing === null) {
return "null\n";
}
if (aThing === undefined) {
return "undefined\n";
}
if (typeof aThing == "object") {
let reply = "";
let type = getCtorName(aThing);
if (type == "Map") {
reply += "Map\n";
for (let [key,value] of aThing) {
reply += logProperty(key,value);
}
}
else if (type == "Set") {
let i = 0;
reply += "Set\n";
for (let value of aThing) {
reply += logProperty('' + i,value);
i++;
}
}
else if (type.match("Error$") ||
(typeof aThing.name == "string" &&
aThing.name.match("NS_ERROR_"))) {
reply += " Message: " + aThing + "\n";
if (aThing.stack) {
reply += " Stack:\n";
var frame = aThing.stack;
while (frame) {
reply += " " + frame + "\n";
frame = frame.caller;
}
}
}
else if (aThing instanceof Components.interfaces.nsIDOMNode && aThing.tagName) {
reply += " " + debugElement(aThing) + "\n";
}
else {
let keys = Object.getOwnPropertyNames(aThing);
if (keys.length > 0) {
reply += type + "\n";
keys.forEach(function(aProp) {
reply += logProperty(aProp,aThing[aProp]);
});
}
else {
reply += type + "\n";
let root = aThing;
let logged = [];
while (root != null) {
let properties = Object.keys(root);
properties.sort();
properties.forEach(function(property) {
if (!(property in logged)) {
logged[property] = property;
reply += logProperty(property,aThing[property]);
}
});
root = Object.getPrototypeOf(root);
if (root != null) {
reply += ' - prototype ' + getCtorName(root) + '\n';
}
}
}
}
return reply;
}
return " " + aThing.toString() + "\n";
}
如您所见,对于类实例(typeof aThing == "object"
),它将调用Object.getOwnPropertyNames
并显示每个属性。
请注意,toString
无济于事-它不适用于对象(类型为aThing ==“ object”)
类似地,在Chrome中V8ValueStringBuilder.append
if (value->IsObject() && !value->IsDate() && !value->IsFunction() &&
!value->IsNativeError() && !value->IsRegExp()) {
v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value);
v8::Local<v8::String> stringValue;
if (object->ObjectProtoToString(m_context).ToLocal(&stringValue))
return append(stringValue);
}
/**
* Call builtin Object.prototype.toString on this object.
* This is different from Value::ToString() that may call
* user-defined toString function. This one does not.
*/
V8_WARN_UNUSED_RESULT MaybeLocal<String> ObjectProtoToString(
Local<Context> context);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。