November 2019
Beginner
804 pages
20h 1m
English
Here's how you can define fields, methods, visibility, and constructors in your TypeScript classes:
class ColoredCar {
private _color: string;
private static DEFAULT_COLOR = "Red";
constructor(color: string) {
this._color = color;
}
displayColor() {
console.log(`Color of this car: ${this.color}`);
}
public get color(): string {
return this._color;
}
public set color(color: string) {
this._color = color
}
private resetColor() {
this._color = ColoredCar.DEFAULT_COLOR;
}
}
As you saw earlier, methods are not prefixed by the function keyword.
Read now
Unlock full access