Last active
July 25, 2018 12:20
-
-
Save anhkind/94b2241fcbbb170a6d7a45a1eddfb71a to your computer and use it in GitHub Desktop.
Typescript - get/set property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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