Ako v TypeScripte "nasimulovat" immutable triedy?

heretik

Ako v TypeScripte "nasimulovat" immutable triedy?
« kdy: 15. 02. 2018, 18:05:52 »
toto:

Kód: [Vybrat]
type Person(firstName, lastName) =
    member p.FirstName = firstName
    member p.LastName = lastName
    member p.FullName = sprintf "%s %s" firstName lastName

som sa v TypeScripte snažil vyjadriť nejako takto:

Kód: [Vybrat]
function Person(firstName: string, lastName: string) {

class Person {

readonly firstName = firstName;
readonly lastName = lastName;
get fullName() { return `${firstName} ${lastName}`; }

}

return Person;
}

lenže to neni úplne to isté nakolko to neni trieda ale "šablóna" tiredy nemôžem ju extendnúť... Ďediť môžem až výsledok funkcie.


Natix

Re:Ako v TypeScripte "nasimulovat" immutable triedy?
« Odpověď #1 kdy: 15. 02. 2018, 18:30:37 »
A co takhle?

Kód: [Vybrat]
class Person {
  constructor(readonly firstName: string, readonly lastName: string) {}

  get fullName() {
    return `${this.firstName} ${this.lastName}`;
  }
}