Typescript classes make traditional object oriented programming easier to read and write. In this lesson we learn about class syntax, what the constructor is and some interesting variable features.
interface Opponent { health: number; alias: string; } class ComicBookCharacter { private team: { name: string; members: ComicBookCharacter[] }; static createAndAssignTeam(teamName: string, members: ComicBookCharacter[]) { let team = { name: teamName, members: members }; members.forEach((member)=>{ member.team = team; }) } constructor(public alias: string, public health: number, public strength: number, private secretIdentity: string) { } getTeamName(){ console.log(`${this.alias} is from ${this.team.name}`); return this.team.name; } attackFunc(opponent: Opponent, attackWith) { opponent.health -= attackWith; console.log(`${this.alias} attacked ${opponent.alias}, who's health = ${opponent.health}`); return opponent.health; } getSecretIdentity() { if (this.secretIdentity) { console.log(`${this.alias} is ${this.secretIdentity}`); } else { console.log(`${this.alias} has no secret identity`); } } } const Sparky = new ComicBookCharacter("Sparky", 6700, 3000, "Thunder"); const Rainer = new ComicBookCharacter("Rainer", 7500, 3400, "Water"); ComicBookCharacter.createAndAssignTeam('Eevee', [Sparky,Rainer]); Sparky.getTeamName();
To review, we've learned about access modifiers and the difference between public
and private
. The constructor is run when the class
instance is initialized, and the shorthand for setting class properties allows us to write less code. We've learned that static
properties can only be referenced from the class, not the instance, and how static
properties have access to an instances
private properties.
Typescript classes make traditional object oriented programming easier to read and write. In this lesson we learn about class syntax, what the constructor is and some interesting variable features.
interface Opponent { health: number; alias: string; } class ComicBookCharacter { private team: { name: string; members: ComicBookCharacter[] }; static createAndAssignTeam(teamName: string, members: ComicBookCharacter[]) { let team = { name: teamName, members: members }; members.forEach((member)=>{ member.team = team; }) } constructor(public alias: string, public health: number, public strength: number, private secretIdentity: string) { } getTeamName(){ console.log(`${this.alias} is from ${this.team.name}`); return this.team.name; } attackFunc(opponent: Opponent, attackWith) { opponent.health -= attackWith; console.log(`${this.alias} attacked ${opponent.alias}, who's health = ${opponent.health}`); return opponent.health; } getSecretIdentity() { if (this.secretIdentity) { console.log(`${this.alias} is ${this.secretIdentity}`); } else { console.log(`${this.alias} has no secret identity`); } } } const Sparky = new ComicBookCharacter("Sparky", 6700, 3000, "Thunder"); const Rainer = new ComicBookCharacter("Rainer", 7500, 3400, "Water"); ComicBookCharacter.createAndAssignTeam('Eevee', [Sparky,Rainer]); Sparky.getTeamName();
To review, we've learned about access modifiers and the difference between public
and private
. The constructor is run when the class
instance is initialized, and the shorthand for setting class properties allows us to write less code. We've learned that static
properties can only be referenced from the class, not the instance, and how static
properties have access to an instances
private properties.