Javascript Typescript:使用fromJson方法指定的对象,如何测试私有属性是否存在并设置它
我有一个包含fromJson方法的对象。由于无法访问类的私有属性,此方法不起作用?出了什么问题以及如何处理?代码是用TypeScript编写的Javascript Typescript:使用fromJson方法指定的对象,如何测试私有属性是否存在并设置它,javascript,typescript,ecmascript-6,Javascript,Typescript,Ecmascript 6,我有一个包含fromJson方法的对象。由于无法访问类的私有属性,此方法不起作用?出了什么问题以及如何处理?代码是用TypeScript编写的 class Example { private Foo: string; // does not matter if private or public, same effect, and normaly has to be private constructor(input?: string) { if (!!inpu
class Example {
private Foo: string; // does not matter if private or public, same effect, and normaly has to be private
constructor(input?: string) {
if (!!input) {
this.foo = input;
}
}
set foo(value: string) {
this.Foo = value;
}
get foo(): string {
return this.Foo;
}
public static fromJson(obj: Object) {
let result: Example = new Example();
for (let index in obj) {
if (Example.hasOwnProperty(index)) { // never runs because false
result[index] = obj[index];
}
/* allready tried this -> same result */
// if (result.hasOwnProperty(index)) {
// result[index] = obj[index];
//}
// let descriptor = Object.getOwnPropertyDescriptor(Example, index); // = undefined
// let descriptor = Object.getOwnPropertyDescriptor(result, index); // = undefined
}
return result;
}
public toJsonString() {
return JSON.stringify(this);
}
public toJsonObject() {
return JSON.parse(this.toJsonString());
}
}
let a = new Example('one');
let json = a.toJsonObject(); // this looks exactly like my api response (type json)
let obj = Example.fromJson(json);
console.log(json);
console.log(obj);
但是console.log(obj)
必须是{“Foo”:“one”,Foo(…)}
编辑:生成的JavaScript:
var Example = (function () {
function Example(input) {
if (!!input) {
this.foo = input;
}
}
Object.defineProperty(Example.prototype, "foo", {
get: function () {
return this.Foo;
},
set: function (value) {
this.Foo = value;
},
enumerable: true,
configurable: true
});
Example.fromJson = function (obj) {
var result = new Example();
for (var index in obj) {
if (Example.hasOwnProperty(index)) {
result[index] = obj[index];
}
}
return result;
};
Example.prototype.toJsonString = function () {
return JSON.stringify(this);
};
Example.prototype.toJsonObject = function () {
return JSON.parse(this.toJsonString());
};
return Example;
}());
var a = new Example('one');
var json = a.toJsonObject(); // this looks exactly like my api response (type json)
var obj = Example.fromJson(json);
console.log(json);
console.log(obj);
我认为这就是你正在寻找的解决方案。如果您用未定义的属性初始化属性,那么toJson方法不会列出参数。因此,您的请求流量没有那么大。生成的javascript看起来像什么?@Arg0n通过edit质疑如果将值分配给
Foo
,生成的JS会发生什么<代码>私有Foo:string=null代码>氩气是正确的。必须初始化每个属性才能通过hasOwnProperty函数访问。我发布了一个工作样本,这正是我想要的。现在初始化我所有模型中的所有属性是一件愚蠢的手工工作;)
class Example {
private Foo: string = undefined;
private Foo2: number = undefined;
constructor(input?: string) {
if (!!input) {
this.foo = input;
}
}
set foo(value: string) {
this.Foo = value;
}
get foo(): string {
return this.Foo;
}
set numeric(value: number) {
this.Foo2 = value;
}
get numeric(): number {
return this.Foo2;
}
public static fromJson(obj: Object) {
let result: Example = new Example();
for (let index in obj) {
if (result.hasOwnProperty(index)) {
result[index] = obj[index]; // care, has to be result
}
}
return result;
}
public toJsonString() {
return JSON.stringify(this);
}
public toJsonObject() {
return JSON.parse(this.toJsonString());
}
}
let a = new Example('one');
let json = a.toJsonObject();
let obj = Example.fromJson(json);
console.log(json);
console.log(obj);