...

/

Solution Review: Counting Animals

Solution Review: Counting Animals

This lesson will explain the solution to the problem in the previous lesson.

Solution #

Press + to interact
class Animal {
constructor() {}
static count = 0;
static increaseCount() {
this.count += 1;
}
static getCount() {
return this.count;
}
}
function test(){
Animal.increaseCount();
console.log(Animal.getCount());
}
test()

Explanation #

This challenge is quite simple if you have a sound understanding of static methods and variables. Static properties are assigned to the class instead of its prototype. This is similar to assigning a property directly to a class. For example, the static method static getCount

static
...
Access this course and 1400+ top-rated courses and projects.