28.5.1. Classes
Wed Aug 31 2022 21:04:47 GMT+0000 (Coordinated Universal Time)
Saved by
@cruz
#javascript
class Astronaut {
name: string;
constructor(firstName: string, lastName: string) {
this.name = firstName + " " + lastName;
}
greet() {
return "Hello, " + this.name;
}
}
let Bob = new Astronaut("Bob","Smith");
//
//You may remember the this and new keywords from working with classes in JavaScript. Earlier in the chapter, we also noted that when declaring variables in TypeScript, we have to specify the type of value. The same applies to function parameters, as you can see in the constructor.
class Panthera {
roar: string;
constructor(currentRoar: string) {
this.roar = currentRoar;
}
}
class Tiger extends Panthera {
stripes: boolean = true;
}
let tigger = new Tiger("loud");
console.log(tigger.roar);
console.log(tigger.stripes);
content_copyCOPY
https://education.launchcode.org/intro-to-professional-web-dev/chapters/typescript/classes.html
Comments