Day26: TypeScript Learning : TypeScript and ES6 Classes
In this article, we will explore how TypeScript enhances the use of ES6 classes, including class properties, methods, and inheritance.
Day26: TypeScript Learning : TypeScript and ES6 Classes
In this article, we will explore how TypeScript enhances the use of ES6 classes, including class properties, methods, and inheritance.
If there are any mistakes, please feel free to leave comments. Thank you!
Class Properties
In TypeScript, you can define properties directly in the class. For example:
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
const person = new Person("John", 30);
console.log(person.name); // JohnClass Methods
You can also define methods within a class:
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.name}`);
}
}
const person = new Person("John", 30);
person.greet(); // Hello, my name is JohnInheritance
TypeScript supports class inheritance, allowing you to create subclasses:
class Employee extends Person {
employeeId: number;
constructor(name: string, age: number, employeeId: number) {
super(name, age);
this.employeeId = employeeId;
}
displayId() {
console.log(`Employee ID: ${this.employeeId}`);
}
}
const employee = new Employee("Jane", 28, 123);
employee.greet(); // Hello, my name is Jane
employee.displayId(); // Employee ID: 123Thank you for reading! See you tomorrow!
References
Related Posts
Day30 :I finally finished!
I finally finished! Recently, I went to draw, and here is a drawing of "The Little Prince and the Fox." My favorite part of "The Little Prince" is their story. Drawing is really therapeutic, just like the feeling of finishing this challenge.
Day29 :TypeScript Learning : React + TypeScript Todo App Part2
Continuing with the Todo app part 2, I will document the issues encountered during the implementation.
Day28 :TypeScript Learning : React + TypeScript Todo App Part1
After taking so many notes, it's finally time to implement a simple to-do app and document the issues encountered during the implementation.