Javascript Typescript:如何使用默认getter显示类的属性
我有一个接口和一个类,如下所示Javascript Typescript:如何使用默认getter显示类的属性,javascript,angular,typescript,getter-setter,Javascript,Angular,Typescript,Getter Setter,我有一个接口和一个类,如下所示 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.p
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: (...)
当我使用console.log(示例)
时,如何使输出仅显示{propsA:“A”,propsB:“B”}
PS:我正在使用
typescript 3.8.3
和Angular 9
您的类示例
有一个名为示例
的类属性,它继承了ISample
。也就是说,很明显你得到了道具的日志:{propA:A,propB:B}
如果希望propA
和propB
成为类的直接元素,则必须正确继承接口。这样做,您必须将propA
和propB
设置为Sample
的直接元素,从而生成所需的日志
export class Sample implements ISample {
propsA = '';
propsB = '';
}
请记住,只要使用
private
states,就必须相应地调整setter和getter。您可以重写toString()方法:
如果您希望直接使用属性,而中间没有任何类对象实例,则可以跳过
示例
类。相反,您可以尝试直接使用ISample
接口
试试下面的方法
导出接口很简单{
普罗帕:弦;
propB:字符串;
}
导出类AppComponent{
sample={}作为示例;
恩戈尼尼特(){
this.sample.propA='A';
this.sample.propB='B';
//印刷品{propA:“A”,propB:“B”}
console.log(this.sample);
}
}
通过控制台.log
您几乎无法更改类的显示。此功能在浏览器中实现,几乎没有操作空间。比如说
如您所见,对于类实例(typeof aThing==“object”
),它调用object.getOwnPropertyNames
,并显示每个属性
请注意,toString
没有帮助-它不用于对象(typeof aThing==“object”)
同样,在铬中
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);
}
if(value->IsObject()&&&!value->IsDate()&&&!value->IsFunction()&&
!value->IsNativeError()&&!value->IsRegExp()){
v8::Local object=v8::Local::Cast(值);
v8::本地字符串值;
if(object->ObjectProtoString(m_上下文).ToLocal(&stringValue))
返回append(stringValue);
}
/**
*在此对象上调用builtin Object.prototype.toString。
*这与可能调用
*用户定义的toString函数。这个没有。
*/
V8_警告_未使用_结果可能是本地ObjectProtoString(
当地环境);
另请参见:完美!我不知道为什么我以前从未想过这件事。谢谢:)
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";
}
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);