Using JSON.stringify in conjunction with TypeScript getter/setter

前端 未结 4 985
小蘑菇
小蘑菇 2020-12-11 01:12

I am using getter/setter accessors in TypeScript. As it is not possible to have the same name for a variable and method, I started to prefix the variable with a lower dash,

4条回答
  •  粉色の甜心
    2020-12-11 02:11

    I think iterating through the properties and string manipulating is dangerous. I would do using the prototype of the object itself, something like this:

    public static toJSONString() : string {
        return JSON.stringify(this, Object.keys(this.constructor.prototype)); // this is version class
    }
    

提交回复
热议问题