【问题标题】:Typescript: specified object with fromJson method, how to test if private property exists and than set itTypescript:使用 fromJson 方法指定对象,如何测试私有属性是否存在并设置它
【发布时间】:2017-04-05 22:01:15
【问题描述】:

我有一个包含 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 (!!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);

【问题讨论】:

  • 生成的javascript是什么样子的?
  • @Arg0n 通过编辑提出质疑
  • 如果给Foo赋值,生成的JS会怎样? private Foo: string = null;
  • ArgOn 是对的。每个属性都必须初始化才能通过 hasOwnProperty 函数访问。我发布了一个工作样本

标签: javascript typescript ecmascript-6


【解决方案1】:
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 方法不会列出参数。所以你的请求流量不是很大。

【讨论】:

  • 这正是我想要的。现在初始化我所有模型中的所有属性是一项愚蠢的手工操作;)
猜你喜欢
  • 2016-11-09
  • 2016-08-24
  • 1970-01-01
  • 2011-02-18
  • 1970-01-01
  • 2021-09-29
  • 2010-10-28
  • 2018-08-01
  • 1970-01-01
相关资源
最近更新 更多