mercredi 6 décembre 2017

TypeScript - getOwnPropertyNames() - Constructor Properties vs. Getter/Setter

I am quite new to TypeScript and there is a point I do not quite understand.

Imagine the following classes:

export class PropertyInConstructor {

  constructor(public foo: boolean) {

  }
}


export class PropertyWithGetSet {

  private _foo: boolean = false;

  get foo(): boolean {
    return this._foo;
  }

  set foo(theFoo: boolean) {
    this._foo = theFoo;
  }
}

From my understanding these two approaches both provide me a property that I can access using new PropertyInConstructor().foo or new PropertyWithGetSet().foo.

I now want to get the existing properties of such a class (without instance!) and to try it out:

console.log(Object.getOwnPropertyNames(PropertyInConstructor.prototype));
console.log(Object.getOwnPropertyNames(PropertyWithGetSet.prototype));

["constructor"]
["constructor", "foo"]

Why is it that the call where the properties are specified inside the constructor does not add the "foo" property?

Is there something missing or some other way to get those properties?





Aucun commentaire:

Enregistrer un commentaire