2017-04-05 20 views
1

我有一個包含fromJson方法的對象。此方法不起作用,因爲該類的私有屬性無法訪問?什麼是錯誤的以及如何處理它?代碼是用TypeScript編寫的。Typescript:使用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); 
+0

生成的JavaScript看起來像什麼? – Arg0n

+0

@ Arg0n通過編輯提出問題 – TypedSource

+2

如果爲'Foo'賦值,生成的JS會發生什麼? '私人Foo:string = null;' – Arg0n

回答

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方法不會列出參數。所以你的請求流量不是那麼大。

+0

這正是我所期待的。現在,在我的所有模型中初始化所有屬性是一個愚蠢的手段;) – TypedSource

相關問題