Last active
November 3, 2021 10:45
-
-
Save arnoldc/41cd44e86f5fced37ce2b4e38115722c to your computer and use it in GitHub Desktop.
[Typescript] Access modifiers
This file contains hidden or 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
| // make sure access modifier are stated (e.g public , private) | |
| // adding readonly for not needed to be modified | |
| export class User { | |
| public readonly id: number; | |
| public firstName: string; | |
| public lastName: string; | |
| public email: string; | |
| protected dob: Date; | |
| public get fullName(): string { | |
| return `${this.firstName} ${this.lastName}`; | |
| } | |
| public doesEmailMatch(email: string): boolean { | |
| return this.email === email; | |
| } | |
| } | |
| // creating private method accesible in the class | |
| export class Admin extends User { | |
| public readonly yearBorn: number; | |
| constructor(firstName: string, lastName: string, email: string) { | |
| super(); | |
| this.firstName = firstName; | |
| this.lastName = lastName; | |
| this.email = email; | |
| this.yearBorn = this.getYear(); | |
| } | |
| private getYear(): number { | |
| return this.dob.getFullYear(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment