Skip to content

Instantly share code, notes, and snippets.

@anhkind
Last active July 25, 2018 12:20
Show Gist options
  • Save anhkind/94b2241fcbbb170a6d7a45a1eddfb71a to your computer and use it in GitHub Desktop.
Save anhkind/94b2241fcbbb170a6d7a45a1eddfb71a to your computer and use it in GitHub Desktop.
Typescript - get/set property
class User {
private _birthDate: string;
// can behave like a LAZY property
get birthDate(): string {
if (this._birthDate) {
return this._birthDate;
}
return this._birthDate = '1981-11-11';
}
set birthDate(value: string) {
this._birthDate = value;
}
}
// usage
const user = new User();
user.birthDate // => '1981-11-11'
user.birthDate = '1982-12-12' // => '1982-12-12'
user.birthDate // => '1982-12-12'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment