Using Static Properties To Have Them At Class Level
[!INFO]-
topic: π¦ TypeScript
links:source: TypeScript course by the native web
tags: #permanent-noteΒ #published
Last Modified:
=dateformat(this.file.mtime, "MMM. dd, yyyy - HH:mm")
To access properties or functions from class level, you have to define them as static
.
class Dog {
public readonly name: string;
public static numberOfDogs: number = 0;
private constructor (name: string) {
Dog.numberOfDogs += 1;
this.name = name;
}
public static bringToLife (name: string): Dog {
return new Dog(name);
}
public bark (): void {
console.log('Woof woof!');
}
}
const alice = Dog.bringToLife('Alice');
console.log(alice.name);
const malin = Dog.bringToLife('Malin');
console.log(malin.name);
console.log(Dog.numberOfDogs);