【发布时间】:2017-11-07 12:23:21
【问题描述】:
我正在尝试对 Typescript 中的对象进行字符串化,这些对象是使用这样的私有属性实现的
export class Foo {
private _property1:string;
private _property2:string;
private _property3:string;
get property1(): string {
return this._property1;
}
set property1(value: string) {
this._property1 = value;
}
get property2(): string {
return this._property2;
}
set property2(value: string) {
this._property2 = value;
}
get property3(): string {
return this._property3;
}
set property3(value: string) {
this._property3 = value;
}
}
但是当我使用 JSON.stringfy() 时,会读取私有属性并且不会通过 get 和 set 方法。
预期:
{
"property1": "",
"property2": "",
"property3": "",
}
收到:
{
"_property1": "",
"_property2": "",
"_property3": "",
}
注意到该属性带有下划线,但不应该,Json 应该带有在 getter 和 setter 中实现的名称,而不是在私有属性中实现的名称
【问题讨论】:
-
您能举例说明这些方法的用法吗?你在哪里使用
JSON.stringify?
标签: json angular typescript getter-setter stringify