gpt4 book ai didi

javascript - Typescript:使用 fromJson 方法指定对象,如何测试私有(private)属性是否存在并设置它

转载 作者:太空宇宙 更新时间:2023-11-04 15:49:45 25 4
gpt4 key购买 nike

我有一个包含 fromJson 方法的对象。此方法不起作用,因为无法访问类的私有(private)属性?出了什么问题以及如何处理?代码是用 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 (!!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)必须是<Example> {"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);

最佳答案

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);

我认为这就是您正在寻找的解决方案。积极的效果是,如果您使用 undefined 初始化属性,则您的 toJson 方法不会列出参数。所以你的请求流量并没有那么大。

关于javascript - Typescript:使用 fromJson 方法指定对象,如何测试私有(private)属性是否存在并设置它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43232396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com