What is a class?

We create a class with the class keyword.

What data type is a class? We can do a simple test to verify this.

index.js
1
classPerson{
2
3
}
4
5
console.log(typeofPerson)
6
7
//function
8

We see in the console that the type is a function.

A class is a function which enables you to create objects. These objects can share similar traits.

One way is to think of this function is an ice tray. You can make as many ice cubes as you want with just one tray.

Each time you use the tray to make more cubes, you create a new object. We say we are creating a new instance of that class. Each ice cube is a new instance.

Creating a new instance

We create a new instance of a class by using the new keyword.

index.js
1
classPerson{
2
3
}
4
5
constMarilyn=newPerson();
6
constDavina=newPerson();
7
constJudy=newPerson();
8
9
console.log('typeofMarilyn',typeofMarilyn);
10
//typeofMarilynobject
11

We see from the console log that each time we create a new instance of a class, we are creating a new object.

Constructor

Classes can have attributes. These attributes can be anything. The only thing a class MUST have is a constructor.

A constructor is a method, which is called automatically every time a new instance of that class is created.

When we think of a person, we think of attributes like name and hair color. Let's give our Person these attributes.

index.js
1
classPerson{
2
constructor(){
3
this.name='Marilyn';
4
this.hairColor='brown';
5
}
6
}
7
8
constanonymousPerson=newPerson();
9

How to access attributes

We know that each new instance of a class is an object. So each attribute we have given our Person is actually a key, with a value. The key is 'name' and the value is 'Marilyn'.

To access these attributes, we do so in the same way you would to access a key:value pair for an object- using object notation.

To access the name attribute of our anonymousPerson, we would write:

index.js
1
classPerson{
2
constructor(){
3
this.name='Marilyn';
4
this.hairColor='brown';
5
}
6
}
7
8
constanonymousPerson=newPerson();
9
10
console.log(anonymousPerson.name);
11
//Marilyn
12

If we create more instances of a class, do they all share the same attributes? Let's check:

index.js
1
classPerson{
2
constructor(){
3
this.name='Marilyn';
4
this.hairColor='brown';
5
}
6
}
7
8
constanonymousPerson=newPerson();
9
constMarilyn=newPerson();
10
constDavina=newPerson();
11
constJudy=newPerson();
12
13
console.log(anonymousPerson.name,Marilyn.name,Davina.name,Judy.name);
14
//MarilynMarilynMarilynMarilyn
15

But what if we want the value of name to be different for every new instance of a class? This is where the constructor comes in.

A constructor defines which parameters the class is expecting, and how those values will be used within the class.

index.js
1
classPerson{
2
constructor(name,hairColor){
3
this.name=name;
4
this.hairColor=hairColor;
5
}
6
}
7
8
constanonymousPerson=newPerson('whoknows?');
9
constMarilyn=newPerson('MarilyntheGreat');
10
constDavina=newPerson('DavinatheFearless');
11
constJudy=newPerson('JudytheWise');
12
13
//whoknows?MarilyntheGreatDavinatheFearlessJudytheWise
14

Default Values

To pass a default value in a regular function, we would use the assignment operator and write something like this:

index.js
1
constcalculateArea=(length=2,width=5)=>returnlength*width;
2

As a class is also a function, we can do the same thing:

index.js
1
classPerson{
2
constructor(name='Marilyn',hairColor='blue'){
3
this.name=name;
4
this.hairColor=hairColor;
5
}
6
}
7
8
constanonymousPerson=newPerson('whoknows?','brown');
9
constMarilyn=newPerson('MarilyntheGreat','grey');
10
constDavina=newPerson('DavinatheFearless');
11
constJudy=newPerson('JudytheWise');
12
13
console.log(anonymousPerson.hairColor,Marilyn.hairColor,Davina.hairColor,Judy.hairColor);
14
15
//browngreyblueblue
16

'Blue' is the default hair color. When we created 'anonymousPerson' and 'Marilyn', we passed in the arguments for hairColor so they have been used by the class when creating the object.

'Davina' and 'Judy' were not passed arguments for hairColor, so the defaults have been used.

Methods

A method is what we call a function, when it is a property of a class:

index.js
1
classPerson{
2
constructor(name='Marilyn',hairColor='blue'){
3
this.name=name;
4
this.hairColor=hairColor;
5
}
6
7
eat(foodItem){
8
console.log(this.name,'iseating',foodItem)
9
}
10
}
11
12
13
constanoymousPerson=newPerson()
14
15
anoymousPerson.eat('Jollof');
16
17
//MarilyniseatingJollof
18

Inheritance

To make classes reusable, we can create new classes which have the same properties of another class.

To create a class which inherits the attributes of another class, we use the extend keyword.

index.js
1
classPerson{
2
constructor(name='Marilyn',hairColor='blue'){
3
this.name=name;
4
this.hairColor=hairColor;
5
}
6
}
7
8
classAthleteextendsPerson{
9
10
}
11
12
constanonymousPerson=newPerson('whoknows?','brown');
13
constMarilyn=newPerson('MarilyntheGreat','grey');
14
constDavina=newPerson('DavinatheFearless');
15
constJudy=newPerson('JudytheWise');
16
17
constMo=newAthlete();
18
19
console.log(Mo.name,Mo.hairColor);
20
//Marilynblue
21

We can see here that our new class Athlete inherits from the Person class.

Mo, which is an instance of Athlete, which itself inherits from Person, has all the attributes of the Person class.

super()

How do we give our new class attributes which the parent does not have?

In our first class, we simply used the this keyword to give our class attributes. Let's try the same with the child class:

index.js
1
classPerson{
2
constructor(name='Marilyn',hairColor='blue'){
3
this.name=name;
4
this.hairColor=hairColor;
5
}
6
}
7
8
classAthleteextendsPerson{
9
constructor(){
10
this.height=10;
11
}
12
}
13
14
constMo=newAthlete();
15
16
console.log(Mo.height);
17

When we run this code, we get the following error:

inherited class error

super must be called before you can access this on the child class.

Static Methods

A static method is a method which can only be called on the class itself, not by any of its instances.

index.js
1
classPerson{
2
constructor(name='Marilyn',hairColor='blue'){
3
this.name=name;
4
this.hairColor=hairColor;
5
}
6
7
staticeat(foodItem){
8
console.log(this.name,'iseating',foodItem)
9
}
10
}
11
12
13
constanoymousPerson=newPerson()
14
15
Person.eat('Jollof');
16
//PersoniseatingJolloff
17
18
anoymousPerson.eat()
19
//TypeError:anoymousPerson.eatisnotafunction
20