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



我有一个包含fromJson方法的对象。 此方法不起作用,因为无法访问类的私有属性? 出了什么问题,如何处理?代码是用打字稿编写的。

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 方法不会列出参数。 因此,您的请求流量没有那么大。

最新更新